I am trying to build an application in node and am trying to implement the login functionality. Below is my code.//login route app.post("/login",function(req,res){ var uname = req.body.uname; var passd = req.body.password; var flag = 0; /*A1*/ connection.query('SELECT * FROM `users`',function(error,results,fields){ results.forEach(function(tmp){ //console.log(tmp,passd); bcrypt.compare(passd, tmp.password,function(err,result){ if(result==true) { flag = 1; res.json(JSON.stringify(tmp)); } }); }); }); /*A1*/ /*A2*/ res.send("No match found"); }); Now the problem that I am facing is that the line A2 should only execute after the A1 section has executed, and furthermore if A1 is executed A2 should not run, as I can only send one of the responses res.send or res.json otherwise it breaks my app. I know callbacks are the answer to my needs, but I can't get it to work using callbacks. Please tell me how to go about solving this (code appreciated). Any help appreciated, also I am a newbie, sorry for any obvious mistakes.
Submitted April 04, 2019 at 11:49AM by kysSolutions
No comments:
Post a Comment