I am using a library that requires me to pass in a "validate" function. In this function I need to validate some input and then return true if it passes or false if it fails.
The problem is that I need to check if a user exists in the database for this validation function and Mongoose invokes a callback with the result. If I return within the callback it's just returning from the callback function not my validate function.
...
validate: function (username) {
User.findOne({ username: username }, function (err, user) {
if (user)
// User exists so we should return false from the validate function
else
// User does not exist so we should return true from validate.
});
}
I'm trying to use the async library to solve this, but async also invokes a callback when its done!