0

I am trying to use Typescript with Express and Mongoose. So far the result has been amazing. I am however stuck at a very minor part.

Premise: I am executing a Mongoose Query using EXEC()

let result = await UserModel.User.find().exec();

I have to use the async / await as there is some processing after this line and I want to avoid callbacks throughout

Problem

I need to get the {err, data} from the result object returned by the query. However currently it simply holds the entire data and I am not able to perform the error handling

So need a way to get the mongoose error description when I use async/await

2
  • Did you promisify this method : UserModel.User.find().exec(); . Doesnt this return a callback ?? Commented Apr 1, 2017 at 5:54
  • Am avoiding the callback by using async and await Commented Apr 1, 2017 at 7:14

2 Answers 2

4

Error handling using async/await is done by using try/catch:

try {
  let result = await UserModel.User.find().exec();
  ...
} catch(err) {
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the library await-to-js

Example:

const to = require(‘await-to-js’).default

const [err, result] = await to(func()) if (err) throw err

...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.