0

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.

  • why is `function crearPedido` even `async`? you never `await`, there's no promises? and payload is only set a value inside the callback, so that's where the value is available - you also never return a value from `crearPedido` so it's not like whatever is calling it needs a returned value, so just do the processing in the callback as you currently do - TL;DR - the only thing your code does "wrong" is expect the LAST `console.log(jsonPayload)` to do anything useful – Bravo Mar 09 '22 at 04:27
  • Good point... i`ll add the next part of the code so you'll get it @Bravo. The thing is, I can't use await inside that callback because it throws an error (only can be used on top bodies of modules) – Willys_fueguino Mar 09 '22 at 04:30
  • oh, you use `jsonPayload` later? Put the code that is "later" inside the callback – Bravo Mar 09 '22 at 04:31
  • Yes... this is the error I get by doing that (using the await code inside the callback): SyntaxError: await is only valid in async functions and the top level bodies of modules – Willys_fueguino Mar 09 '22 at 04:34
  • @elclanrs yes... But I don't have the level to process all the technical info on that answers, so I wish someone can help me understanding this with a "for dummies" answer... I think I can bypass this by creating a separate function to use the await on the top level without messing too much with the funcion purpose. – Willys_fueguino Mar 09 '22 at 04:43
  • well, post the code that has an issue, not code that has no issue – Bravo Mar 09 '22 at 04:53

0 Answers0