Home > Enterprise >  Variable becomes undefined after passing it as an argument (vue js to js)
Variable becomes undefined after passing it as an argument (vue js to js)

Time:12-16

I have the following functions:

view.vue

await this.newShelf(payload, token)

shelves.js

async newShelf({ commit }, payload, token) {

But when I try to use token in newShelf it becomes undefined for some reason but if I print it in the vue file it just prints the value of token. How can I make this work?

CodePudding user response:

payload is being deconstructed into { commit }

await this.newShelf(objectWithCommit, payload, token)
//or
async newShelf(payload, token) {}
//or
async newShelf({ commit }, token) {}

You need to remove one of these (I don't know what var are you using)

CodePudding user response:

commit - Mutation commit 
State - State Variable
Payload - Request Parameter

We can pass token as payload parameter

async newShelf({commit, state}, payload) {

}

CodePudding user response:

You can pass the payload as an object:

let payloadObject = {
  payload: paylaod,
  token: token
}

then use it like this

await this.newShelf(payloadObject)
async newShelf({ commit }, payload) {
  payload.payload // your payload
  payload.token  // your token
}

CodePudding user response:

You can pass it like this

await this.newShelf({otherData, token})

and can access it like this

async newShelf({ commit }, obj) {
  • Related