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

Only the last useFetch fires in a component with multiple fetches #14725

Closed
jas777 opened this issue Aug 27, 2022 · 3 comments
Closed

Only the last useFetch fires in a component with multiple fetches #14725

jas777 opened this issue Aug 27, 2022 · 3 comments

Comments

@jas777
Copy link

jas777 commented Aug 27, 2022

Environment

------------------------------
- Operating System: `Windows_NT`
- Node Version:     `v16.13.1`
- Nuxt Version:     `3.0.0-rc.8`
- Package Manager:  `yarn@1.22.17`
- Builder:          `vite`
- User Config:      `head`, `css`, `plugins`, `publicRuntimeConfig`, `build`
- Runtime Modules:  `-`
- Build Modules:    `-`
------------------------------

Reproduction

https://stackblitz.com/edit/github-2cwqjm-gdnhe4?file=app.vue

Describe the bug

When more than one useFetch is used in a component - only the last one fires and all other calls return null except for the first time the page is loaded.

Additional context

I've only tested fetches in onMounted, but I don't know whether it's relevant to the issue

Logs

No response

@TheDutchCoder
Copy link
Contributor

I can confirm this issue as well!

@ShaggyTech
Copy link

ShaggyTech commented Aug 28, 2022

I believe you'll have to use Promise.all() like so:

onMounted(async () => {
  const [{ data: r1 }, { data: r2 }] = await Promise.all([fetch1(), fetch2()]);

  d1.value = r1;
  d2.value = r2;
});

Forked your example with the fix:

https://stackblitz.com/edit/github-2cwqjm-qtsgju?file=app.vue

See also this discussion:
#12774

@danielroe
Copy link
Member

Let me reiterate my comment in the linked discussion. Do not use useFetch within a click handler or equivalent. Like all composables, it must be run within an appropriate context. Issue to follow for adding a guide to composables: #14723.

In your case, use $fetch directly to perform programmatic data fetching.

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
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

4 participants