Skip to content

Cannot get Async data to work with Pinia + PreFetch #14028

Discussion options

You must be logged in to vote

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:

actions: {
  async fetchData() {
    const { data } = await siteDataService.getSiteCopy();
    this.siteCopy_ = data;
  },
},

Inside preFetch:

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.

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@gregbengis
Comment options

Comment options

You must be logged in to vote
1 reply
@DominicLee
Comment options

Answer selected by yusufkandemir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants