I have a small function to validate the user input. In that function I will also check if the email address is already taken. But I have some trouble with the async callback.
I hope anyone could give me a hint, how I could solve this. I use nodejs with the express 3 framework, mongodb and the node-validator library.
This is a part of my validation function:
function check(data, callback) {
mongoose.model('User', User).count(data, function (err, count) {
callback(err, !! count);
});
};
function validate(email, password, confirm) {
var v = new Validator(),
errors = new Array();
v.error = function (msg) {
errors.push(msg);
};
check({email: email}, function (err, exists) {
v.check(exists, { email: 'E-Mail is already taken' }).equals(false);
});
...
return errors;
}
Based on the async callback the variable errors is at the return statement empty. How could I else check if the email address is already in the databse?
You need to think async and pass callbacks that will be called asynchronously.
Quick example: