OK I am doing this in a node.js file. I have a function that is suppose to search for a user in mongodb, based off a certain fields. It is suppose to capture their account value and then pass it on for further evaluation. However the account variable remains null. It looks like this:
function checkIfUserExist (name, lobby, socket) {
var account_ID = null;
Clients.findOne({ user_name_perm: name }, function (err, result) {
if (err) {
console.log("Cannot find user with perm name: checkIfUserNameExist");
} else {
if (result == null) {
...} else {
console.log("User perm name is: " + result.user_name_perm);
console.log("result.account_ID: " + result.account_ID);
account_ID = result.account_ID;
}
}
});
console.log("Account ID: " + account_ID);
if (!account_ID == null) {...} else {
console.log("User doesn't exist");
}
}
The log portion looks like this:
Account ID: null
User doesn't exist
User perm name is: Peter
result.account_ID: 15555555555
This is very confusing to me, it seems like the function is evaluating everything but the database look up first, which makes account_ID null for evaluation purposes, then it goes into the database and makes the new evaluation after the fact, maybe, why is it doing this?!