N
N
newkoss2021-02-25 18:54:51
Vue.js
newkoss, 2021-02-25 18:54:51

Why does [vuex] unknown mutation type: give me an error?

Gives an error message[vuex] unknown mutation type: setUser

const state = {
    user:{

    }
},
    getters = {

    },actions = {
    getUser(){
        axios.get("/api/v1/user/current")
            .then(response=>{
                this.commit('setUser', response.data);
            })
    },
    loginUser({}, user){
        axios.get('/sanctum/csrf-cookie').then(response => {
            axios.post('/api/v1/user/login', {
                email: user.email,
                password: user.password
            }).then(response=>{
                console.log(response.data);
                localStorage.setItem(
                    "token",
                    response.data.access_token
                )
            })
        });
    }
},
mutations = {
    setUser(state,data){
        state.user = data;
    }
};

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}


created() {
            axios.defaults.headers.common['Authorization'] = "Bearer "+localStorage.getItem('token');
            this.$store.dispatch('currentUser/getUser');
        }


I would be very grateful for the help, otherwise I don’t have the strength to look for what is the reason ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
�
ⓒⓢⓢ, 2021-02-25
@newkoss

I think this is it:
this.commit('setUser', response.data);
looking for a mutation from the root store.
try something like this:

getUser({ commit }){
        axios.get("/api/v1/user/current")
            .then(response=>{
                commit('setUser', response.data);
            })
    },

or specify the full path (along with the module name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question