2

This is my code:

db.Registration.model.countDocuments()
.then(function (count) {
    return res.status(200).send({ count });
})
.catch(function (err) {
    console.log('errrr', err);
    if (err)
        return res.status(500).sendStatus(err);
    else
        return res.status(200).sendStatus({ count });
})

I get the following error:

Express server running on *:5000
(node:8864) UnhandledPromiseRejectionWarning: Error: querySrv ENOTFOUND _mongodb._tcp.cluster0-shard-00-00-jjbdh.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19)
(node:8864) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8864) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Why catch is not working and I get this error instead?

Elia Weiss
  • 8,324
  • 13
  • 70
  • 110
  • Perhaps the error is thrown from within the `catch` clause. Did you try listening to `unhandledRejection` events? It might give you more information. https://stackoverflow.com/questions/43834559/how-to-find-which-promises-are-unhandled-in-node-js-unhandledpromiserejectionwar – OzW Apr 08 '19 at 12:00
  • 1
    The exec doesn't reach the `catch` clause, otherwise u would see 'errr' in the output – Elia Weiss Apr 09 '19 at 05:21
  • Thanks for the link, I tried to implement it, but it doesn't catch the error – Elia Weiss Apr 09 '19 at 05:22
  • If the error is thrown from the catch clause it will be an unhandled rejection. The catch clause will not catch its own errrors. – OzW Apr 09 '19 at 05:28
  • The error here is clearly not from the catch clause; it's related to the underlying query the promise is running. I'm running into a similar issue and don't have a suggestion yet (get unhandled promise rejection from promise that has a catch clause. In my case though, the catch clause is run ok, does not throw an error, but unhandled is still triggered. :( – Samuel Neff Sep 14 '21 at 15:08

0 Answers0