I am trying to query user by email and save the result inside a variable. I tried using await and async, callback and promise but none of them actually work.
const checkEmailUsed = (email) => {
let sql = "SELECT * FROM users_signup WHERE user_email = ? LIMIT 1";
return new Promise((resolve, reject) => {
conn.query(sql, email, (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res[0]);
});
conn.end();
});
};
var user = [];
checkEmailUsed("mikias@email.com")
.then((res) => user.push(res))
.catch((err) => console.log(err));
console.log(user); // Still []
Best Answer
The console.log is executed before the promise is fulfilled. This should work: