如何解决如何将变量发送到中间件
我有一条快速路由器路线,如下所示:
.post(
authController.protect,itemController.createItem,//create Item
itemController.sendNotification('new-item'),//if user exists send notification
);
createItem控制器具有一些可以使用type('new-match')运行sendNotifications的逻辑,如果被跳过,则将使用next()
继续执行下一个控制器,如下所示:
if(foo) next()
else sendNotifications('new-match')
我的sendNotifications中间件定义如下:
const sendNotification = type =>
catchAsync(async (req,res,next) => {
if (req.userFound) {
await Notification.create({ user: req.userFound,notType: type });
const user = await User.findByIdAndUpdate(req.userFound,{
$inc: { notifications: 1 }
});
req.userFoundEmail = user.email;
}
next();
});
exports.sendNotification = sendNotification;
我的问题是,当从路由栈中引入sendNotifications中间件时,它将可以访问req,next
,但是从控制器本身调用的req,next
是undefined
我如上所述从控制器内部调用sendNotification:
sendNotification('new-match');
我如何通过两种方式提供对req,res,next的访问?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。