Answer the question
In order to leave comments, you need to log in
How to use async/await in actions vuex?
I looked at a bunch of answers to this question, but for some reason it doesn’t work in the end:
in sore.js
actions: {
async initialCityList({ commit }) {
const siteContacts = await fetch('/api/contacts')
commit('setSiteContacts', siteContacts)
}
},
async created() {
await this.$store.dispatch("initialCityList");
},
new Promise .....
everything is fine.
Answer the question
In order to leave comments, you need to log in
in general figured out, if it will be useful to anyone: it await fetch()
returns the Response
object
, respectively, it is required to execute await response.json()
or other methods, depending on the type of data received. I draw attention to await: response.json()
it returns a Promise, respectively, await is needed
Total:
actions: {
async initialCityList({ commit }) {
const siteContacts = await fetch('/api/contacts')
commit('setSiteContacts', await siteContacts.json())
}
},
actions: {
async initialCityList({ commit }) {
const siteContacts = await axios.get('/api/contacts')
commit('setSiteContacts', siteContacts.data)
}
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question