Search code examples
javascriptvue.jsvuejs3vuex

Vue 3: How to call function from another function in the same mutations.js in Vuex store


In my mutations.js file, I happen to have one function calling another function in the same file. Here's my sample code example:

export default {
  async addQuestionAnswer(state, payload) {
    alert(payload);
    this.updateSubjects(state);
    alert("This is never reached");
  },
  updateSubjects(state) {
    alert("This is never reached");
  },
}

Does anyone know how to call the updateSubjects function from the addQuestionAnswer function? I've checked my console.log and found this error:

Uncaught (in promise) TypeError: _this.updateSubjects is not a function

Solution

  • Thanks @kissu, I've found the solution to my problem based on the console.log error I've posted with your suggestion. Thanks. I added this to my mutations.js file:

    import store from '../store';
    

    Then I changed this:

    this.updateSubjects(state);
    

    To this:

    store.commit("updateSubjects");
    

    And it works! :-)