Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncData and $fetch not waiting for /server/api call to be finished #27209

Closed
Jesus82 opened this issue May 14, 2024 · 3 comments
Closed

asyncData and $fetch not waiting for /server/api call to be finished #27209

Jesus82 opened this issue May 14, 2024 · 3 comments

Comments

@Jesus82
Copy link

Jesus82 commented May 14, 2024

Environment

  • Operating System: Darwin
  • Node Version: v20.13.1
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: npm@10.5.2
  • Builder: -
  • User Config: components
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Clone this repo and npm run dev or check this codesandbox

Go to index page and click link (video)

Describe the bug

I have an endpoint in the /server/api folder that sends this JSON as a response.

{
    "title": "A random title",
    "slug": "a-test-slug",
}

I have a TestCard component that receives an object as a prop:

<template>
  <p>{{ simplePost.title }}</p>
</template>

<script setup>
const props = defineProps({
  simplePost: {
    type: Object,
    required: true,
  },
})
</script>

In my /test page, I fetch this data with asyncData and fetch as recommended in the docs, and then I send that object as a prop:

  <template>
    <div>
      <nuxt-link :to="'/'">Index</nuxt-link>
      <TestCard :simple-post="simplePost" />
    </div>
  </template>
  
  <script setup>
  
  const { data: simplePost } = useAsyncData('simplePost', async () => {
    const post = await $fetch(`http://localhost:3000/api/simple-post`)
    return post
  })
  </script>

When trying to browse to /test via a nuxt-link, it seems that the page rendering doesn't wait for asyncData. Then when refreshing the /test page, it works correctly.

Additional context

No response

Logs

[Vue warn]: Invalid prop: type check failed for prop "simplePost". Expected Object, got Null
@manniL
Copy link
Member

manniL commented May 14, 2024

That's correct and expected. You can use await useAsyncData to ensure it will wait or handle the null case separately (which is preferred, e.g. via v-if)

@manniL manniL closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
@warflash
Copy link
Member

Seems like you forgot awaiting the useAsyncData call: https://stackblitz.com/edit/github-q9fuc6?file=pages%2Ftest%2Findex.vue,pages%2Findex.vue

@Jesus82
Copy link
Author

Jesus82 commented May 14, 2024

OMG, totally missed that (wasn't expecting to use await without an async)

@manniL if you could make that clarification stand out in the docs, I guess it would save lot of time for the next one that falls into this. Thanks for the quick reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants