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

12,273

Solution 1

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

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

Solution 2

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

Share:
12,273
Liam_1998
Author by

Liam_1998

Updated on June 11, 2022

Comments

  • Liam_1998
    Liam_1998 almost 2 years

    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)
    }
    
  • Ondřej Ševčík
    Ondřej Ševčík about 5 years
    Thanks, what a stupid mistake. I was looking for an issue in form or in the payload but after while I noticed that my mutation type wasn't defined.
  • John
    John over 4 years
    This is not an answer.
  • Gil Shapir
    Gil Shapir over 4 years
    John - as the same error message received when having CAPS mismatch - and not quotes issue - IMHO this is another answer
  • DrCord
    DrCord about 3 years
    Which happens if the constant you are trying to use is undefined or mispelled, a gotcha to watch for...
  • Damilola Olowookere
    Damilola Olowookere over 2 years
    @DrCord That's exactly why you should use constants for mutation types