Can anyone tell me what's wrong with this code? When I console.log from within the getUserByEmailQuery
function it gives me the user_id however when I return the result it is null. I can't seem to figure out why.
Here's my code:
function getUserByEmailQuery(email, callback) {
const sql = "select * from users where email = ?;";
con.query(sql, [email], function (err, result) {
if (err) throw err;
console.log("checking id within: " + result[0].user_id); // prints - checking id within: 2
return callback(result);
});
}
function getUserByEmail(email) {
var user = null;
getUserByEmailQuery(email, (result) => {
user = result;
});
console.log("checking id outside: " + user[0].user_id); // TypeError: Cannot read property '0' of null
}
Any help is greatly appreciated. I can provide the larger context of what I'm doing if necessary.