0

How can I use mongoose promises with the vscode code runner extension. Consider the code below.

async function getUsers(){
    return await User.find({}).exec()
}

console.log(getUsers()) //returns Promise { <pending> }

Why is it returning a promise? Yet I have used async/await? Plus according to the mongoose docs using exec() makes a query a promise. How can I get the resolved promise?

YulePale
  • 6,688
  • 16
  • 46
  • 95
  • You've made a [very similar question](https://stackoverflow.com/questions/62486046/vs-code-how-can-i-use-the-run-code-command-to-query-my-database), stating that your code worked fine and it was a Visual Studio Code issue. `.exec()` must return a promise to work with [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await). The fact that you're using VSCode to run Node doesn't change this. – Álvaro González Jun 20 '20 at 16:01
  • What I meant by saying is that, the code has no issue yet I am not getting the required results. Await and async work, the issue seems to be with the mongoose query. – YulePale Jun 20 '20 at 16:05

0 Answers0