Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
Getting data out of a callback in async function

I'm trying to make my code work (it actually works in another part of my code, IDK why it isn't working here). Any help or advice would be greatly appreciated.

exports.crearPedido = async function crearPedido(req, res, next){

let {email, codProducto, cantidad, direccion, formaPago, estado } = req.body;
let emailHeaders = req.headers.email
let emailCorrecto = ""
let jsonPayload = undefined
let token = req.headers.authorization
let userEmail
//console.log(codProducto)
if (email === undefined) {
 emailCorrecto = emailHeaders
} else if(emailHeaders === undefined) {
 emailCorrecto = email
} else if(emailHeaders === undefined && email === undefined){
 token = req.headers.authorization.split(" ")[1];
 jwt.verify(token, process.env.JWT_SECRET_KEY, (err, authData) => {
     
     let base64Url = token.split('.')[1];
     let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
     jsonPayload = decodeURIComponent(Buffer.from(base64, 'base64').toString().split('').map(function(c) {
         return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
     }).join(''));
     console.log(jsonPayload)
     jsonPayload = jsonPayload.split(':')[1]
     jsonPayload = jsonPayload.split(',')[0]
     jsonPayload = jsonPayload.split('"')[1]
     jsonPayload = jsonPayload.split('"')[0]
     console.log(jsonPayload)
   })
}else {
 res.status(403).json("Error de credencial de usuarios")
}
console.log(jsonPayload)
userEmail = await usuarios.findOne({auth0Id: {$eq: jsonPayload}} ,{'_id': false})

This is Part of my code, not all of it, but I guess you guys can get an idea of what's my point. The last console.log returns "undefined" or whatever data I initialize that variable with, but the one before returns the proper data. It seems like the jsonPayload variable is changed within the jwt.verify function but gets back to whatever data I use to initialize it outside of it.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!