[Vuejs]-State is undefined inside of vuex action

0👍

Well, Remove export from state, actions & mutation as you’re already exporting them in the end with export default.

const state = {
  playlist: {},
  oldTitle: '',
  oldDescription: ''
}

const actions = {
  async edit({ commit, state }, payload) {
    try {
      console.log(state) // **undefined?!?!**
      const updated = await PlaylistService.update(state.playlist.guid, {
        title: payload.title,
        description: payload.description
      })
      commit('edit', updated)
    } catch (error) {
      console.error(error)
    }
  }
}

const mutations = {
  openEdit(state, payload) {
    state.playlist = payload
    state.oldTitle = payload.title
    state.oldDescription = payload.description
  },
  edit(state, payload) {
    state.playlist = payload
  },
  cancelEdit(state) {
    state.playlist.title = state.oldTitle
    state.playlist.description = state.oldDescription
  }
}

Hope this helps!

Leave a comment