Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this console error showing after this Vuex mutation commit?

I’m quite new with Vue and Vuex, I’m having this console error after committing a declared Mutation in the store, which by the way, besides from the console error is doing its job. The error which is: ReferenceError: ADD_EVENT is not defined at eval (EventCreate.vue?89e7:82:1), it’s pretty much self-explanatory.
I’m probably missing one step or calling something in the wrong place but it all seems to be in place, so here’s my code:
the store:

import { createStore } from 'vuex'

export default createStore({
  state: {
    user: 'TommyDemian',
    events: []
  },
  mutations: {
    ADD_EVENT(state, event){
      state.events.push(event);

    }
  },
  getters: {
  },
  actions: {
  },
  modules: {
  }
})

The component committing the mutation:

export default {
  name:'EventCreate',
  setup () {
    const store = useStore();

    const onSubmit = () => {
      event.organizer = store.state.user;
      event.id = uuidv4();
      apiClient.postEvent(event).then(() => {
        store.commit(ADD_EVENT, event);
      })
      .catch((error) => console.log(error));
    };
  
      const event = reactive ({
        id: '',
        category: '',
        title: '',
        description: '',
        location: '',
        date: '',
        time: '',
        organizer: ''
      });

      return {
        onSubmit,
        categories,
        event,
        store,
      }
    }
  }
</script>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should dispatch action which commit mutation:

actions: {
  setEvent({ commit }, event) {
    apiClient.postEvent(event)
      .then((response) => {
        commit('ADD_EVENT', response);
      })
      .catch((error) => console.log(error));
  },
};
  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading