0
userFoundId = undefined;
const result = connection.query('SELECT id FROM users WHERE name = :name and
password = :password', data, (err, result) => {
userFoundId = result[0].id //Valor alterado para 1
});
connection.end();
console.log(userFoundId) //Display undefined
How can I perform a user search inside the mysql database and the result of the query be saved in the userFoundId variable that will be used to log in?
Basically, you can’t. Like the
connection.query
is asynchronous, the callback will be executed after theconsole.log(userFoundId)
, that is on the outside of the callback. Read this answer and this to learn more.– Luiz Felipe
Does this Answer your Question? How to assign the callback variable "cursor.toArray(err, doc)" to an external variable?
– rLinhares