I'm trying to build a custom validator for my express application. Here it is
app.use(validator({
customValidators:{
isAvalible:function(userName){
console.log("userName as param = " + userName)
//if username is not present in database return True else False
var query = user.where({userName:userName})
query.findOne(function(err,user){
if(err){
console.log("error occured in findOne()")
return handleError(err)
}
if(user){
console.log("user = " + user)
if(user == null){
return true
}
else{
return false
}
}
})
console.log("returning")
return true;
}
}
}));
It's functionality is to check if a userName inserted into a registration form is alredy in use by some other user.
Query.findOne() is an async function i belive, thus the above function logs me "userName" and immediatly after "returning". It dosent even enter the findOne()'s callback, thus results are not set properly. How could i fix this behaviour?