Home > OS >  Uncaught (in promise) TypeError: Cannot create property 'token' on string
Uncaught (in promise) TypeError: Cannot create property 'token' on string

Time:12-20

Am trying to implement authentication using vuex. I have a register component and also a auth.js for submiting data.

My backend API are working fine but the issue is when i try to login. When i console log in action method am able to get the token.

when i console log before the SET_TOKEN mutation am also able to get data.

But in mutations the data is received like an object. How do I solve the error?

import axios from "axios"

export default{
    namespaced: true,
    state: {
        token: null,
        user: null
    },
    
    mutations: {
        SET_TOKEN(access_token, state){
            console.log(this, access_token)
            state.token = this,access_token
        },
    },
    
    actions: {
        async loginUser({ dispatch }, form) {
            let response = await axios.post('/api/auth/login', form)
            dispatch('attempt', response.data.data.access_token)
        },

        async attempt({ commit }, access_token){
            commit('SET_TOKEN', access_token)
        }
    }
}

CodePudding user response:

I think you mixed up the order of mutation parameters a little bit. First is state, second is payload (token in your case)

SET_TOKEN(state, token) {
  state.token = token;
},
  • Related