0

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,

Tijmi
  • 45
  • 8

1 Answers1

-1

you have to return like this.

const emailverification = async (req, res) => {


var tokens = req.tokens;
  return db.query(
    "SELECT * FROM users WHERE token = ?",
    [tokens],
    async (err, result) => {
      if (err) throw err;
      if (result[0].verify == 0) {
        console.log(result[0].email);
        if (result.length > 0) {
          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 res.status(200).send({ message: "true" });
        } else {
          var message = { status: "error", error: "email already verified" };
          console.log("5");
          return res.status(400).send({ message: "false" });
        }
      } else {
        console.log(result);
        console.log("6");
        message = { status: "error", error: "email already verified" };
        return res.status(400).send({ message: "false" });
      }
    }
  );
};
module.exports = emailverification;
Moman Raza
  • 155
  • 1
  • 5
  • I triedd it and it still doesn't waiot for the things and the `result[0]` is now unidefined – Tijmi Jan 26 '23 at 10:54
  • I have update my answer try now. Just log result not result[0] and let me know for any issue. You have to get token from req header previously you getting req in token. – Moman Raza Jan 27 '23 at 09:36
  • the function now doesn't get the token anymore – Tijmi Jan 27 '23 at 15:12
  • you have to send token in req headers when you call api through front end or from postman. – Moman Raza Jan 27 '23 at 19:36