I have this code, which prints the correct result. I only want "example@gmail.com" in a variable:
[ RowDataPacket { CORREO: 'example@gmail.com' } ]
conexion.query("SELECT CORREO FROM ALUMNOS WHERE NOMBRE='RUBEN'", function (error, results, fields) {
if (error) {
throw error;
}else{
console.log(results)
}
})
But when I want to save it in a variable, it gives me an error or undefined:
async function obtainEmail() {
let result = await conexion.query("SELECT CORREO FROM ALUMNOS WHERE NOMBRE='RUBEN'", function (error, results, fields) {
if (error) {
throw error;
}else{
return results;
}
})
}
obtainEmail().then(data => console.log(data));
Result:
undefined
I have also tested:
let test = await obtainEmail();
console.log(test);
Result:
SyntaxError: await is only valid in async functions and the top level bodies of modules
Trying to save the query result ("example@gmail.com") in a variable