I really need help with that I spent a lot of time trying to understand that but I don't get it. I'm using nodeJS and MongoDB
I want to do a function that returns true or false depending on if the email is in the database:
I have done that
function isUserRegistered(email) {
MongoClient.connect(url, function (err, db) {
if (err) throw err;
let database = db.db(dataBaseName);
let query = { "email": email };
var userCollection = database.collection(collectionName)
userCollection.find(query).toArray(function (err, result) {
if (err) throw err;
db.close();
if (result.length > 0) {
return true;
} else {
return false;
}
});
});
}
console.log(isUserRegistered("jack@gmail.com").toString());
But when I test it doesn't work because isUserRegistered() is undefined at the time I try to print it. I have tried to do callback functions, and I achieved to print either "false" or "true" with that, but with a callback function, I'm only starting a new function and the isUserRegister() doesn't return a boolean, so I can't do something like that later on my program:
if (isUserRegister(email){
// Try to login
} else {
// try to create the account
}
I have also look for async and await, but I don't know where I should use it. Can it be fixed in a simple way?
Thank you