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.

`useFetch` data return null with mismatching client/request options

See original GitHub issue

Environment

------------------------------
- Operating System: `Linux`
- Node Version:     `v16.14.2`
- Nuxt Version:     `3.0.0-27480123.4c77c88`
- Package Manager:  `yarn@1.22.18`
- Builder:          `vite`
- User Config:      `-`
- Runtime Modules:  `-`
- Build Modules:    `-`
------------------------------

Reproduction

In clean Nuxt project:

<template>
  <div>
    <NuxtWelcome />
  </div>
</template>

<script lang="ts" setup>
const [{ data: organization }, { data: repos }] = await Promise.all([
  useFetch(`https://api.github.com/orgs/nuxt`),
  useFetch(`https://api.github.com/orgs/nuxt/repos`)
])

console.log(organization.value) // null

const { data: organization } = await useFetch(`https://api.github.com/orgs/nuxt`)
console.log(organization.value) // null
</script>

Describe the bug

useFetch started returning null to the “data” property

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:23 (20 by maintainers)

github_iconTop GitHub Comments

3reactions
pi0commented, Apr 1, 2022

For giving manual key and baseURL to useFetch: (notice baseURL case)

await useFetch(`/api/v2/...`, { baseURL, key: 'countries-api' })

This will make keys same.


Interesting find @danielroe!

I would still assume it is a bug that the client-side is returning null instead of re-attempt to fetch!

It is not safe to remove baseURL or any other option from auto key generation. Two instances of useFetch with different baseURL will conflict this way.

Thinking about some possible improvements. Supporting proxy for me seems best approach. In the meantime, we should improve documentation and warn about this that if URL or options between client/server are not same, useFetch needs explicit key or will need to refetch (currently mismatch)

3reactions
danielroecommented, Apr 1, 2022

Ah. I’ve just seen what you’re doing with baseURL. The key is based on the URL so by changing the URL between server and client, there is a mismatch.

In your case, you could use useAsyncData and pick a key manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt 3 useFetch sometimes returns null - Stack Overflow
If an error occurred while fetching (e.g. timeout), data will be null . You can either handle this in your template: <div v-if="data"> ......
Read more >
useFetch
Options ; data, Allows you to set a default value for data, undefined ; interceptors.request, Allows you to do something before an http...
Read more >
Fetch Standard
A request has an associated reserved client (null, an environment, or an environment settings object). Unless stated otherwise it is null.
Read more >
Server configuration - FilePond - PQINA
Asynchronously uploading files with FilePond is called processing. In short, FilePond sends a file to the server and expects the server to return...
Read more >
10. Node.js: HTTP, HTTPS - Books at mixu.net
Server request Methods setEncoding(encoding=null) pause() resume() Events data end ... this will print out all the different properties of the current HTTP ...
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