I have the following function:
const findUserByEmail = (email) => {
var user
MongoClient.connect(url, (err, db) => {
const dbo = db.db('users')
dbo.collection('users').findOne({email: email}, (err, result) => {
if (err) throw err
else return result
})
db.close()
}).then((user) => {
return user
})
}
I am trying to return the value of user so that findUserByEmail(email)=user but can't figure out how. I have tried using async/await and promises to return this value to get around Node's asynchronous nature however in each I could not get the value to return to the main function. In this case, return user returns the user to the then() function, which is not correct.
Any help would be much appreciated.