I have a login code which sends data (input by the user) to a mysql database , matches the password and returns a status based on the success or failure of the verification. For this I have a function defined. The status variable is declared inside the function and updated through a few if statements which verify the password. However, when the function is called, it always returns the default value of the status
This is the code:
var con = mysql.createConnection({
host: "localhost",
user: "XXXXXX",
password: "XXXXXX",
database: "XXXXXX"
});
con.connect(function(err){
if(err) throw err;
});
function confirm(uname,pass){
var query = "select * from clients where name='"+uname+"' ";
var stat = 0;
con.query(query,function(err,result){
if(err) throw err;
if(result.length<1){
stat=4;
}
var password = result[0].password;
if(password === pass){
console.log('verified');
stat=1;
}
if(password!=pass){
stat=2;
}
});
return stat;
}
The problem is that the console.log('verified')
gets executed when the condition is fulfilled. But when this function is called it always returns 0
How do I make it update the stat
variable?