OscarDev
OscarDev

Reputation: 977

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);
        },
}

Upvotes: 3

Views: 2447

Answers (2)

Jam3si
Jam3si

Reputation: 51

I had the same problem, it was because of vuex-persistedstate package. As @OscarDev mentioned, change of variable name helped. It is because persistedstate saved the type of variable when you were writing the code. So clearing the persistedstate cache helped too.

Upvotes: 2

Riddhi
Riddhi

Reputation: 2244

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.

Upvotes: 3

Related Questions