Async/Await Mongoose doesn't always run correctly Async/Await Mongoose doesn't always run correctly mongoose mongoose

Async/Await Mongoose doesn't always run correctly


I don't know the specifics on that library or method, but I can tell you why it's not working.

"await" will pause only when the right hand side of the statement returns a "Promise" object. In your code example, it seems that the function takes a callback. Callbacks, though asynchronous, are not promises. Perhaps you can check that library's API docs to see if it can return a Promise instead of taking the callback?


You are mixing callbacks with async-await. Don't do that. Please study how they work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Your route handler should be:

exports.GetAllUrls = async function(req, res, next){  try {      const urlsArray = await Url.find({uid: req.params.uid}).exec()      return res.status(200).json({reply: urlsArray});  } catch(err) {      console.log(err);  }}
  1. .find() returns a Query object: http://mongoosejs.com/docs/api.html#find_find
  2. .exec() returns a Promise: http://mongoosejs.com/docs/api.html#query_Query-exec