margherita pizza
margherita pizza

Reputation: 7145

vuex doesn't recognize the mutation

This is my vuex root store.

export default new Vuex.Store({
  state: {
    account: {}
  },
  mutations: {
    set_account(state,payload) {
      console.log("I AM INSIDE MUTATIONS", )
    }
  },
  actions: {
    setAccount({
      commit
    }, payload) {
      return new Promise((resolve, reject) => {
        commit(set_account, payload)
        resolve()
      })
    }
  },
  plugins: [vuexLocal.plugin],
  modules: {
    auth,
    update
  }
});

Here after the set account action I commit set_account mutation. But it says,

set_account is not defined

Why could this happen?

Upvotes: 1

Views: 105

Answers (1)

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38777

Try using commit() with type, the first argument, being a string equal to 'set_account', the name of the respective action function name:

export default new Vuex.Store({
  state: {
    account: {}
  },
  mutations: {
    set_account(state,payload) {
      console.log("I AM INSIDE MUTATIONS", )
    }
  },
  actions: {
    setAccount({
      commit
    }, payload) {
      return new Promise((resolve, reject) => {
        commit('set_account', payload)
        resolve()
      })
    }
  },
  plugins: [vuexLocal.plugin],
  modules: {
    auth,
    update
  }
});

From the Vuex documentation for commit:

commit(type: string, payload?: any, options?: Object)

type, the first argument should be a string, which in this case would need to match the function name of set_account. Without it being a string, it's attempting to evaluate the expression set_account which is effectively undefined in the current execution context.

Hopefully that helps!

Upvotes: 3

Related Questions