I am attempting to use JavaScript and MongoDB to check if a username is present in a dataset. I am looking to have it return a simple true or false statement. I am using the following syntax:
checkUserName = function (usernameIn) {
User.findOne({username: usernameIn}, function(err, item) {
if (err) throw err;
if(item === null) {
console.log("Does not exist")
return false;
}
else {
console.log("Username already exists.")
return true;
}
});
However, when I call the function it only returns undefined instead of the true or false statements.
EDIT:
I do call var User = mongoose.model('Users') at the top of the script. The function does correctly execute the conole.log() operations, however it just does not return a result when I perform var result = checkUserName(username).
Many thanks