I'm trying to use await to wait for a database lookup using mongoose and save the response to a variable. For some reason, when I pass a function to .exec(), then await doesn't seem to work.
Way that doesn't work:
var document = await Collection.findOne({ 'num': num }).populate('field').exec((err, result) => {
if (err) throw error
})
console.log(document) // undefined
Way that does work:
var document = await Collection.findOne({ 'num': num }).populate('field').exec()
console.log(document) // The document I want
I don't think the population is what is causing the problem because I tried the code without the population and it worked. Why does this happen and how can I fix this?