Search code examples
javascriptvuejs2vuex

TypeError: state.categoriesState.push is not a function VUEX


I have this error but I do not understand the reason, could you guide me?

Error:

TypeError: state.categoriesState.push is not a function

My code:

state.js

export default {
    categoriesState: []
}

mutations.js

export function setCategories(state, category){
    state.categoriesState.push(category);    
};

Calling **VUEX from my component:**

methods: {
...mapMutations('cat', ['setCategories']),

        addCategoriesToVuex(category){
            this.setCategories(category);
        },
}

Solution

  • Most probably your variable categoriesState is an object and not an array. You should check that first by doing console of typeOf of your variable categoriesState.

    export function setCategories(state, category){
          console.log(typeOf(state.categoriesState));
        //state.categoriesState.push(category);    
    };
    

    You can use set method to add new property to an object in vue.