I am building a user sign up and crud app. I have a db model.
var valid;
module.exports = {
connectDB: function(){
}
,
findUser: function(req,res) {
}
,
createUser: function(email,name,password){
var role = 'view';
var sql = "INSERT INTO `users`(email, name, password, role) VALUES (" + "'"+email+"'"+","+"'"+name+"'"+","+"'"+password+"'"+","+"'"+role+"'"+")";
db.query(sql, function(err, result){
if(err){
console.log(`FAILED: ${err}`)
return false; // tried setting valid to false
}
else
{
console.log(`User Created`);
valid = true;
}
});
return valid; // this also returns undefined
}
}
I want to be able to return a bool depending up on the succession of the query. I tried returning true/false. I did some searching on why this happens. Firstly there was an explanation about js being asynchronous. My question is what is the correct way of returning values in function when exporting so that I can use it?