L
L
Leo_SAN2021-10-27 12:15:43
Node.js
Leo_SAN, 2021-10-27 12:15:43

How can I access a variable in an async method?

await sendEmail(req.body.email, this.verificationCode) throws an error that this.verificationCode = undefined, how can I fix this?

class VerificationCodeController{
  constructor(){
    this.verificationCode = generateCode()
  }
     async sendCode(req, res){
     await sendEmail(req.body.email, this.verificationCode)
      return res.json(responseCreator.response(this.verificationCode));
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Voznesensky, 2021-11-01
@Wil_Ascension

Are you initializing variables in the constructor? If so, check what the generateCode() function returns

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question