I'm building a Nuxt app and I'm fetching some data from a node backend I've got running on my localhost.
I have a plugin getApps.js
export default ({ store }) => {
store.dispatch('getApps')
}
That is calling a getApps action in my Vuex
actions: {
getApps (context) {
const {commit, state} = context
commit('setLoading', true)
let url = `apps?limit=${state.loadLimit}&page=${state.page}`
if (state.query)
url = `${url}/q=${state.query}`
this.$axios.get(url)
.then((res) => {
const apps = res.data.apps
console.log(apps)
commit('addApps', apps)
commit('setPage', state.page + 1)
commit('setLoading', false)
})
}
...
The console.log here does indeed return the list of apps, however, after my addApps mutation
addApps (state, payload) {
state.apps = payload
}
And this is the state definition
state: () => ({
apps: [],
query: '',
loading: false,
filters: [],
loadLimit: 25,
page: 1,
showFilters: true,
currentUser: null,
showLoginModal: false,
showCreateAppModal: false
})
The state doesn't get updated. As far as I could tell, this is due to the async nature of actions. I did also try to wrap the action around an async and prepend an await to the axios call, however, this did not work.
Why is this happening? How do I have to structure my code to make it work?