Categories
JavaScript Answers

How to fix the UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block error with Node?

Spread the love

To fix the UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block error with Node, we add a catch block.

For instance, we write

router.get("/emailfetch", authCheck, async (req, res, next) => {
  try {
    const emailFetch = await gmaiLHelper.getEmails(
      req.user._doc.profile_id,
      "/messages",
      req.user.accessToken
    );
    res.send(emailFetch.data);
  } catch (err) {
    next(err);
  }
});

to wrap our code that might thrown an error with a try block.

Then when an error is thrown, the code in the catch block will run.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *