I have a very simple route
router.get('/', async (req, res, next) => {
try {
const allEmployees = await employees.find({});
res.json(allEmployees);
} catch (error) {
next(error);
}
});
It works ok. But after I refactored it with catch
. it stopped working and threw UnhandledPromiseRejectionWarning:
router.get('/', async (req, res, next) => {
const allEmployees = await employees.find({}).catch(next)
res.json(allEmployees);
});
It seems like the next
is not called correctly in the second version. But Both should be equivalent in JavaScript. not sure why the second one is broken in Express.