I'm working on a project that uses a node/express API and Mongo for storage. I have a function that tries to retrieve data from the storage using the code in the screenshot below. My understanding of async/await is that at the point of await
, code execution will pause and proceed when the promise is resolved.
However, the data returned by the function (in the screenshot) is always null, although, the record is there in the db. [The slug is also passed correctly.]
I am starting to believe I am missing something regarding the concept of async/await
.
Could anyone please assist me with this?
Am I doing something wrong here?
The calling function is as follows:
async create(req, res, next) {
debug(chalk.blue(`*** Create RSVP`));
console.log(req.body.event); //event is defined and matches db
const event = await Event.findBySlug(req.body.event);
console.log(event); // logs null here
}
Called function:
async function findBySlug(slug) {
return await Model.findOne({ slug: slug })
.populate('user')
.populate('category')
.exec();
}