Cannot get Async data to work with Pinia + PreFetch #14028
-
Hi I've been using Vuex until recently on a number of projects, and with PreFetch, I am able to dispatch an action in my store, which calls a promise, before setting the data. The PreFetched data would then display in my components as expected. I'm now using Pinia with a new project, which until now has been a breeze, but I am implementing the SSR side of things now, and I cannot get Async data to populate my store. It makes sense I guess - I am not sure how it magically worked with Vuex, but it seems Vuex knew that there were outstanding requests which it had to wait for, in order to render the page correctly.
the correct value to the getter 'siteCopy' is set perfectly outside of SSR. However with SSR, the value remains {loading:true} I am trying to initialize the store in 2 ways:
But I can ONLY get my getter value to update if I set it synchronously in Pinia: What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
did u found a solution for this ? |
Beta Was this translation helpful? Give feedback.
-
Inside the store: actions: {
async fetchData() {
const { data } = await siteDataService.getSiteCopy();
this.siteCopy_ = data;
},
}, Inside async preFetch({ store }) {
const siteDataStore = useSiteDataStore(store);
await siteDataStore.fetchData();
} It can't be related to Vuex, it's how asynchronous stuff works in JS. When using Vuex, you were probably returning the Promises, so it was working. |
Beta Was this translation helpful? Give feedback.
preFetch
thinks your code is synchronous because you are not returning any Promise, or made it an async function and await the calls inside it. You could do this instead:Inside the store:
Inside
preFetch
:It can't be related to Vuex, it's how asynchronous stuff works in JS. When using Vuex, you were probably returning the Promises, so it was working.