Error: [vuex] expects string as the type, but found undefined

Total
48
Shares

Studying Vuex. I wrote a simple login page against the example project and the document, but when I tried to use a action function, the developer tool just warned me

error info

Here is my code:

src/views/Login.vue

handleLogin (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // to do
          this.$store.dispatch('user/login', this.loginUser)
        } else {
          ......
          })
        }
      })

src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/User/user'
// import info from './modules/info'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: false,
  modules: {
    user,
    // info
  }
})

/src/store/modules/User/actions.js

export const userActions = {
  login({commit}, loginUser) {
    commit(LOGIN)
    axios.post(`${ API_BASE_USER }/login`, loginUser)
         .then(res => {
           console.log(res)
           if (res.status == 200) { commit(LOGIN_SUCCESS, res.data) }
           else { commit(LOGIN_FAILURE, res.data) }
         })
  }
}

/src/store/modules/User/user.js

import { userActions } from './actions'
import { userMutations } from './mutations'
export default {
  namespaced: true,
  state: {
    token: ''
  },
  actions: Object.assign({}, userActions),
  mutations: Object.assign({}, userMutations)
}

Solution

I got it.
The origin mutations-type.js export const LOGIN = LOGIN

But the correct mutation-type.js should be export const LOGIN = 'LOGIN'

Leave a Reply

Your email address will not be published. Required fields are marked *