您可以在您的API路由中添加以下代码来解决此错误:
router.get('/example', async function(req, res, next) {
try {
// your code here
} catch (error) {
if (error.code === 'NEXT_REDIRECT') {
res.redirect(error.redirectUrl);
} else {
next(error);
}
}
});
此代码 block 会检查错误对象的“code”属性并根据其值执行操作。如果“code”是“NEXT_REDIRECT”,它将通过调用“redirect()”方法重定向到 error.redirectUrl指定的URL。否则,它将调用“next()”方法将错误传递给错误处理程序。