I' m building a email verification for my website in node.js and for it to work it changes a value in a mysql database, it has to return 1 or 0 if it works or doesn't work but the code doesn' t wait for the return and inmidiatly goes further even though I use async
and await
.
this is the code I used in the pages.js:
router.get("/verify-email", async (req,res) => {
var tokens = req.query.token
console.log(tokens)
const verified = await emailverification(tokens)
console.log("hello",verified)
if (verified == 1) {
res.sendFile('verifySuccess.html',{root: "./public"});
} else {
res.sendFile('verifyFail.html',{root: "./public"});
}
})
and this is the funciton it has to wait for:
const emailverification = async(token,req,res,next) => {
var tokens = token
console.log("hello",tokens)
db.query('SELECT * FROM users WHERE token = ?',[tokens], async(err, result) =>{
console.log("1")
if (err) throw err;
console.log(result)
if(result[0].verify == "0"){
console.log("2")
console.log(result[0].email)
if (result.length > 0) {
console.log("2.5")
var email = result[0].email
console.log(email)
console.log("2.75")
db.query('UPDATE users SET verify = 1 WHERE email = ?', email, async(err, result) => {
console.log(result[0])
console.log("3")
if(err) throw err
return 1
})
} else {
console.log("5")
return 0;
}
}else{
console.log("6")
return 0;
}
})
}
module.exports = emailverification;
I searched on goolge and here on stackoverflow and found a lot of stuff about this but it doesn't work in my code.
this is the source code: the source code
the follwing 2 questions: how-do-i-return-the-response-from-an-asynchronous-call and how-to-properly-return-a-result-from-mysql-with-node don't help because those questions ar abour something else and not about the problem I have. because by my problem the code doesn' t return the stuff before contini=uing even though I use the async/awaint things like they do by these 2 questions
please don't mind all the logs I use them to see what runs and what doesn't,