Nuxt.js: Fetching data via store server side not working

Created on 4 Jul 2017  路  5Comments  路  Source: nuxt/nuxt.js

Im sure this has been asked before but i cant for the life of me figure out whats going on

this code in the page

fetch ({ store }) {
 const response = await axios.get(dataUrl);
 console.log(response.data)
 store.commit("SET_SECTIONS", response.data);
}

and yet somehow this doesn't work

fetch ({store}) {
 store.dispatch('fetchSections')
}

where in the store the action is doing the same thing

 async fetchSections({ commit, getters }) {
    const response = await axios.get(dataUrl);
    console.log(response.data)
    commit("SET_SECTIONS", response.data);
  }

the console log is the same for both, but for some reason the one where the fetc calls the action isnt populating the store?

Any ideas?

This question is available on Nuxt.js community (#c903)
help-wanted

Most helpful comment

Hi @keithmancuso, have you tried to use return in the second option?

fetch ({store}) {
 return store.dispatch('fetchSections')
}

All 5 comments

Hi @keithmancuso, have you tried to use return in the second option?

fetch ({store}) {
 return store.dispatch('fetchSections')
}

Ahh interesting that fixed it, thanks for the quick response.

Although I'm not sure i understand why?

It's because fetch() can be or return a Promise :)

right! thank you!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pehbehbeh picture pehbehbeh  路  3Comments

gary149 picture gary149  路  3Comments

bimohxh picture bimohxh  路  3Comments

vadimsg picture vadimsg  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments