1

I'm trying to get the answer from two API routes and depending on the result display the data. But for some reason, when I trying to use more than 1 axios call it doesn't work, failing with 404/500 error.

I've tried following:

<template>
  <div v-if="blogPost">
    <p>post</p>
  </div>
  <div v-else-if="blogCategoryPosts">
    <p>category,posts</p>
  </div>
</template>

<script>
export default {
  async asyncData({ $axios, app, route }) {
    const blogPost = await $axios.get(`${process.env.API_DOMAIN}/api/blog/posts${route.path}`)

    const blogCategoryPosts = await $axios.get(`${process.env.API_DOMAIN}/api/blog/categories${route.path}`)

    return {
      blogPost: blogPost.data,
      blogCategoryPosts: blogCategoryPosts.data,
    }
  },
}
</script>

and

<script>
export default {
  async asyncData({ $axios, app, route}) {
    const [blogPost, blogCategoryPosts] = await Promise.all([
        $axios.get(`${process.env.API_DOMAIN}/api/blog/posts${route.path}`),
        $axios.get(`${process.env.API_DOMAIN}/api/blog/categories${route.path}`),
    ])

    return {
        blogPost: blogPost.data,
        blogCategoryPosts: blogCategoryPosts.data,
    }
  },
}
</script>

Each call works fine separately but together they don't. Any idea why is that?

2 Answers 2

1

You should await your Promise.all like this

const [blogPost, blogCategoryPosts] = await Promise.all([
  $axios.get(`${process.env.API_DOMAIN}/api/blog/posts${route.path}`),
  $axios.get(`${process.env.API_DOMAIN}/api/blog/categories${route.path}`),
])

Also, don't forget the , at the end of the first $axios.


I gave a similar answer here few time ago.


PS: if you want to have those issues fixed quickly, use ESlint.
If you want a tutorial on how to have both ESlint and Prettier, you can follow my tutorial here.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you. I forgot to add `"await"(but it was in my old code)in following example, with it my code don't work anyway.
my code don't work anyway please be more explicit here, does it gives you an error. Also, you did maybe forgot a , as mentioned above.
Yeah, commas in the place. It returns generic "Request failed with status code 500", I'll enable ESlint asap.
@RedEclipse alright, so it's a server issue. Check if there is more details in the Network tab (in your browser devtools), then aim to your backend to check the logs. Probably not a Nuxt issue if everything else is in place.
That what I thought, but API works fine though direct access or using each axios call separately.
|
0

So in my case it was sufficient to point on .then and .catch for axios

  export default {
  async asyncData({ $axios, app, route}) {
    
    const blogPost = await $axios.get(`${process.env.API_DOMAIN}/api/blog/posts${route.path}`).then(response => response.data).catch(error => {})

    const blogCategoryPosts = await $axios.get(`${process.env.API_DOMAIN}/api/blog/categories${route.path}`).then(response => response.data).catch(error => {})

    return {
      blogPost: blogPost,
      blogCategoryPosts: blogCategoryPosts,
    }
  },
  }

Everything worked well. Also I sort of misunderstood 500 error, i thought it a generic message, but my API was just telling me that category not found.

4 Comments

This will be sequential so.
Time has shown that it is imperfect. I have an interceptor app-wide for axios(it catches 500 error), so for example it works good if category exists(and not exists, showing 500 error), but when I'm getting a post inside of category, it displays post but throws 500 error(popup) because no category found, how I could prevent that?
Do you have a public repo for this?
Not yet, I realized that my approach wasn't good. I wanted to keep all my toasts only in a separate axios.js file(have got interceptors for 500 and other errors here), but when I fetch on one page 2 axios instances it'll always drop the error. So I slightly changed the backend when it couldn't find the category from 500 to 404 and stuff now at least works as it should :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.