I have a function in NodeJS that makes a query into Mongo. The function is "checkForMatchingEmail".
var result = checkForMatchingEmail(req.body.email);
if ( result === 1) { // something something }
else if ( result === 0) { // something something }
I want to use the RESULT variable for deciding on what to do next. (sending a response to the front-end whether such an e-mail exists in DB).
The problem is that the query to Mongo seems to lag a bit, so the thread just skips over it and the if/else tree is useless, because the query results are not in yet.
What to do?
EDIT:
checkForMatchingEmail looks like this:
function checkForMatchingEmail(email) {
var result = 0;
User.findOne({email: email}, function(err,obj) {
console.log("email is " + email);
if (err) {
return handleError(err);
} else if (obj != null) {
console.log("This email already exists." + obj);
result = 1;
} else if (obj === null) {
result = 0;
console.log("This email does not exist.");
}
});
return result;
}