简体   繁体   中英

Vuex store - callback is not a function

Why do I keep getting the error "callback is not a function" in this action in my Vuex store?

updateRemoteUser: ({ state, commit }, callback) => {
  const user = state.user
  axios.put(`/users/${user.id}`, { 
    user: user
  })
    .then(response => {
      commit('changeUser', response.data.user)
      callback(true)
    })
    .catch(errors => {
      console.log(errors)
      callback(false)
    })
},

EDIT And then I'm calling the above action like this:

async setResponses() {
  this.userResponse = await this.updateRemoteUser()
  if(this.userResponse) {
    this.$router.push({ name: 'weddingDetails' })
  }
  else {
    console.log("Something is jacked")
  }
},

Check if callback is actually being passed to tee action. A simple check would reveal the answer

if (!callback) console.log('tada');

When you call the mutation...

this.updateRemoteUser({ 
    callback: (e) => console.log('do something') 
})

On Your Vuex

updateRemoteUser: ({ state, commit }, data) => {
   const user = state.user
   axios.put(`/users/${user.id}`, { 
    user: user
  })
 .then(response => {
    commit('changeUser', response.data.user)
     if (data.callback) data.callback(true)
  })
  .catch(errors => {
    console.log(errors)
    if (data.callback) data.callback(false)
  })
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM