Hi all i have created a basic nodesjs server using express. There is a login page on which a user is sent, it checks entry from the Sqlite3 DB. My query is Callback function works only after its parent function is done. However in the following case, since the db.each command from the Db takes some time, callback function doesn't give the desired output. But if I set a timeout function it gives an appropriate answer.
Code snipped /*post request userLogin page is called */
app.post('/userLogin',function(req,res){
checkLogin(req,function(){//using a callback
setTimeout(function(){//delaying the execution of this part**
if(userLoginStatus=='SUCCESS'){res.render('userLogin');}
else{res.send('Wrong credentials');}
},100);
});
});
function checkLogin(req,callback){
db.serialize(function(){
db.each("SELECT * from USERLOGIN where USERID ='"+req.body.userId+"'"+
" AND PASSWORD='"+req.body.password+"'",function(err,row){
if(row.USERID.length>0){userLoginStatus ='SUCCESS';}
else {userLoginStatus = 'UNSUCCESFUL';}
});
});
callback();
}