Reputation:
I am trying to query for a name field in mongodb and with the following code I always get the response that the value is a duplicate.
var checkUserName = function (userName, email, res){
User.findOne({ name : userName }, function () {
if(userName && typeof userName !== 'undefined'){
res.send("duplicate");
}else{
checkEmail(email);
}
});
}
Upvotes: 1
Views: 121
Reputation: 3141
you are currently ignoring the answer from the database completely and are just checking your input again. This cannot work. Mongoose returns the answer of the query as the second parameter of the callback function (the first is always there to signal errors) - try something like this:
var checkUserName = function (userName, email, res){
User.findOne({ name : userName }, function (err, userFromDb) {
if (err) {
console.log("there was an error: " + err;
return res.send("err: " + err.message);
}
if(userFromDb) {
res.send("duplicate");
} else {
checkEmail(email);
}
});
}
Upvotes: 1