Skip to content
Advertisement

Awaiting an async function when using Mongodb in node.js

I am trying to retrieve all documents from a MongoDB cluster. I’ve been searching online and using the async/await keywords and have wrote the following code

JavaScript

From my understanding of the async/await pattern theconsole.log(questions) line should only be hit after the return inside GetQuestions is hit, however that is not the case.

Advertisement

Answer

You should return a Promise with the result of Mongoose:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement