我在NodeJS中有一个中间件异步功能。
module.exports = function (handler) {
return async (req, res, next) => {
try {
console.log('async called...');
await handler(req, res);
}
catch(ex) {
console.log('catch...');
next(ex);
}
};
}
这是我的路线处理程序。
const asyncMiddleware = require('../middleware/async');
router.get('/', asyncMiddleware(async (req, res) => {
await db.query('SELECT * xxx FROM aircraft LIMIT 10',
function (err, rows, fields) {
if(err) throw err;
res.send(rows)
})
}));
问题是,当我使用无效语法调用路由处理程序以模拟异常时,中间件的捕获未从路由中的err捕获异常。
当我呼叫路线时, 异步称为... 但不是 抓住... 在控制台上。
有人知道为什么会这样吗?