Hampus Nilsson
Hampus Nilsson

Reputation: 21

Cant access object values in vueX in actions

I´m writing a method in VueX store to upload a object converted to a json to Firebase. In vuex store under action I have created a method called scheduleTickets(purchase) but in the methods I cant access the value in the purchase object. But If I commit to a mutations function and send the object to it I can access them but not from actions. Am I doing something wrong here?

async scheduleTickets(purchase){
      let documents = JSON.stringify(purchase)
      for(let document of documents){
        console.log(document)
      }
    }

Upvotes: 0

Views: 346

Answers (1)

webprogrammer
webprogrammer

Reputation: 2477

From the docs https://vuex.vuejs.org/guide/actions.html

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

You should have at least 2 arguments: context and second - payload. Override your method to this:

scheduleTickets(context, purchase){
      let documents = JSON.stringify(purchase)
      for(let document of documents){
        console.log(document)
      }
    }

And you can dispatch it like this:

// dispatch with a payload
store.dispatch('scheduleTickets', purchase)

Upvotes: 1

Related Questions