this.$store.dispatch().then() is not working in Vuex

Viewed 6993

I'm using VUEX in my project and I need to perform some action AFTER the state changes. Here is my module on the store.js:

const state = {
    activeEvent: null
};

const mutations = {
    SET_ACTIVE_EVENT(state, activeEvent) {
        state.activeEvent = activeEvent;
    }
};

const actions = {
    setActiveEvent({ commit }, activeEvent) {
        commit("SET_ACTIVE_EVENT", activeEvent);
    }
};

export default {
    state,
    mutations,
    actions
};

Here's the code:

this.$store.dispatch("setActiveEvent", event).then(() => {
   //Something...
});

When I do this, I get this error message from console:

Uncaught (in promise) TypeError: Cannot read property 'then' of undefined

What I've tried so far:

const actions = {
    setActiveEvent({ commit }, activeEvent) {
        return new Promise(resolve => {
            commit("SET_ACTIVE_EVENT", activeEvent);

            resolve(true);
        });
    }
};

And:

const actions = {
    setActiveEvent({ commit }, activeEvent) {
        return new Promise(resolve => {
            resolve(commit("SET_ACTIVE_EVENT", activeEvent));
        });
    }
};

But both return the same error. What am I doing wrong?

Edit:

If it helps, I'm using:

  • vue ^2.5.16
  • vue-electron ^1.0.6
  • vuex ^3.0.1
  • vuex-electron ^1.0.0
0 Answers
Related