I have a client sending JSON information to the server and trying to make a register. JSON includes name and password of this user. Before the server makes the register into the database it needs to make sure that the username is available. For this purpose i created a function called isRegisted(req) that makes a search on the database and if the result.length is greater than zero then set a boolean value to true and at the end of the function returns this boolean value. The mysql search returns what it's expected in "result" but at the end the boolean value is not set and the console.log prints undefined.
Here's my code:
function register(req , res){
res.setHeader('Content-Type', 'application/json');
console.log(isRegisted(req));
}
function isRegisted(req){
var flag;
var query = conn.query("SELECT * FROM Users WHERE name=?", [ req.body.name ], function(err, result) {
if(err){
console.error(err);
return;
}
console.log(result);
if(result.length > 0)
flag = true;
else
flag = false;
});
return flag;
}
Thank you for your attention.