Alright, so here's the situation. I'm working on a node.js file where I've got a function set up to search for a user in mongodb using specific fields. The goal is to retrieve their account value and then pass it along for further processing. But for some reason, the account variable keeps turning out null. Here's a snippet of what I have:
function checkIfUserExist (name, lobby, socket) {
var account_ID = null;
Clients.findOne({ user_name_perm: name }, function (err, result) {
if (err) {
console.log("Can't 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");
}
}
And when I log everything, this is what shows up:
Account ID: null
User doesn't exist
User perm name is: Peter
result.account_ID: 15555555555
This whole process is really puzzling me. It seems like the function is going through all the steps except for the database lookup first, which ends up leaving account_ID as null initially for evaluation purposes. Then after that, it jumps into the database and does the evaluation later. This just doesn't make sense to me, why is it behaving this way?!