0

I have declared variable globally at top of the variable and I am trying to access it inside the other call to do check whether user_id exist or what?. My function is working properly, but it's failing to validate. Please let me know where I am going wrong.

Global Method :

function isUserExist(user_id,callback) {
db.query('select id from user where id = ?' , [user_id],function(error, rows) {
    console.log("i ------>" + user_id); 
if(rows.length >0) {
return true;
}else {
    return false;
}
}); 
} 

inside another method :

router.post('/addcustomerasset', function(req,res) {
    if (create(user_id) ){
        console.log("User exists");
              // Code will execute
    }   
    else{
    response.success = false,
    response.mssg = "User Doesn't Exist";
    res.json(response);
    }
});
Vikash Chauhan
  • 792
  • 2
  • 9
  • 18
phoine
  • 125
  • 1
  • 7
  • 1
    Why are you returning `true` from both branches of an `if`/`else`? Not that it matters, `query` ignores the return value of the callback you pass it. See the linked question's answers for the problem: `query` is **asynchronous**, you can't *return* the result of your check. – T.J. Crowder Nov 17 '17 at 06:29
  • just now i updated the code, it was my typing mistake – phoine Nov 17 '17 at 06:32
  • The main point above remains, though; the linked question's answers answer the question. Happy coding! – T.J. Crowder Nov 17 '17 at 06:32
  • cheers .. @T.J.Crowder – phoine Nov 17 '17 at 06:33

0 Answers0