Check if document exists in mongodb using es7 async/await
Ok, here's how I got it working:
async function userExistsInDB(email, password) { let db = await MongoClient.connect('mongodb://127.0.0.1:27017/notificator'); try { let collection = db.collection('users'); let userCount = (await collection.find( { email: email, password: password }).limit(1).count()); return userCount > 0; } finally { db.close(); }}
And because the async
keyword in function declaration guarantees that the returned value will be a Promise
, the only way to get the real returned result out of this function is:
let result = await this.userExistsInDB(email, password);
inside of another function declared async
.