Liam_1998
Liam_1998

Reputation: 1477

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

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

Upvotes: 13

Views: 16671

Answers (3)

Meme Overlord
Meme Overlord

Reputation: 1037

This can also happen when you call $store.commit() without providing it an argument

Upvotes: 9

Gil Shapir
Gil Shapir

Reputation: 139

Had a similar situation, where the Mutation name started with a CAP (), but the actual mutation started with a non cap(): RetrieveContractorSuccess: 'retrieveContractorSuccess' (before was RetrieveContractorSuccess: 'RetrieveContractorSuccess')

Upvotes: -1

Liam_1998
Liam_1998

Reputation: 1477

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

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

Upvotes: 20

Related Questions