question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useAsyncData does not pass errors from server to client-side

See original GitHub issue

Environment

  • Operating System: Linux
  • Node Version: v14.18.1
  • Nuxt Version: 3.0.0-27294839.7e5e50b
  • Package Manager: yarn@1.22.15
  • Bundler: Vite

Reproduction

https://stackblitz.com/edit/github-ygz9df?file=app.vue

Describe the bug

On server side the error content is rendered as expected while the client side says it’s null.

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:25 (5 by maintainers)

github_iconTop GitHub Comments

21reactions
RiccardoCherchicommented, Apr 1, 2022

After a bit of search trough the ohmyfetch (the library used by nuxtjs mentioned in the documentation) I obtained the status code easily, this is my solution:

<script setup lang="ts">

let errorStatus;
const { data: myData, error: error }: { data: any; error?: any } =
  await useFetch(
    "https://jsonplaceholder.typicode.com/404",
    {
      async onResponseError({ response }) { // onResponseError is a "ohmyfetch" method
        errorStatus = response.status; // assign the error to the declared variable "errorStatus"
      },
    }
  ); 

return { myData, error, errorStatus }; // returning all the variables

</script>

This is my first response to a GitHub issue, hope i wrote it at least understandable

18reactions
danielroecommented, Nov 25, 2021

@nndnha I understand. One note: if you need to get the status code on client side and don’t mind an extra request, you can also rerun the fetch.

const { error, refresh } = await useFetch('https://jsonplaceholder.typicode.com/404');
if (process.client && error.value) {
  await refresh()
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Fetching - Nuxt
Downside: Won't work for server side rendering. Make the API call in the asyncData method of the page component and pass the data...
Read more >
Why useFetch is not working on page change in nuxt3 on ...
useFetch tries to cache the document for you and reuses the cache when you load the next page. To avoid that behavior you...
Read more >
Understanding Difference Between Asyncdata Fetch Nuxt
Fetching data in your application is not just about loading it but also doing so at the right time (i.e. server-side vs. client-side)....
Read more >
Nuxt3 doesn't fetch new data after client-side navigation : r/Nuxt
Or use useAsyncData() and supply a dynamic key value that changes based on your different data you expect to receive (usually an ID,...
Read more >
Nuxt 3 | Data fetching | useAsyncData | useFetch - YouTube
Hi all,Thanks a lot guys for voting in the poll that I ran last week. It was super helpful.As per the majority, here...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found