Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

75
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda