#Pinia, plugins and multiple `useFetch` calls results in 500 Error

25 messages · Page 1 of 1 (latest)

somber ridge
#

Pinia, plugins and multiple useFetch calls results in 500 Error

#

The real-life use case I have for this is fetching a user, if the session is outdated to send another useFetch call to a session-updater endpoint and then rerun the original user fetch.

#

Oh and I realized I didn't mention this, but it works fine with just one useFetch call, which is why I am confused.

spiral pike
#

useFetch is only guaranteed to be available during lifecycle events or setup

#
  async function testFetch() {
    let response = await $fetch('https://jsonplaceholder.typicode.com/users');
    console.log(response);
    response = await $fetch('https://jsonplaceholder.typicode.com/users');
    console.log(response);
  }
somber ridge
#

@spiral pike And yet I don't quite understand why it's not available when I use pinia stores, yet they it is available if I access it directly.

#

Adding to the confusion it for some reason breaks when I try to fire it the second time?

spiral pike
#

because useFetch is not going to be available every time you use a store

#

you should be using $fetch in stores 🙂

somber ridge
#

Alrighty

#

Will have to figure out how to not request data more than once

spiral pike
#

for inspiration 🙂

somber ridge
#

I'll take a look it at

spiral pike
#

basically what you probably want to do is have a data.get(id) that will either return stored data or fetch, store and return new data

somber ridge
#

The main issue is incorprating that into the already existing API request setup I have

#

Either I write a separate request logic for the stores

#

Or find a more general way to structure my api requests

spiral pike
#

that's why i made megamap

#

it's a javascript Map but with caching, loading etc

#

an API connected Map if you will

somber ridge
#

Yeah, I'll take a look at it

#

Not a fan of dependending on stuff, so I'll probably try to think of something myself

spiral pike
#

haha understandable, but you can definitely reference the code 🙂