我有这段代码:
exports.get_transducer_edit = (req, res) => {
if(req.isAuthenticated()){
Transducer.findById(req.params.id, (err, foundTransducer) => {
if(err){
res.redirect('/en/dashboard');
} else {
res.render('dashboard/transducer_edit-dashboard', {transducer: foundTransducer});
}
});
}else{
req.flash('error','You need to log in to do that');
res.redirect('/dashboard/login');
}
};
它运行没有问题,但是当我在middleware / index.js中创建一个中间件时:
var middlewareObj = {};
middlewareObj.isLoggedIn = function(req, res, next){
if(req.isAuthenticated() ) {
return next();
}
res.redirect('/dashboard/login');
};
module.exports = middlewareObj;
我在这段代码中调用了它:
const middleware = require('../middleware');
const Transducer = require('../models/productTransducers');
exports.get_transducer_edit = middleware.isLoggedIn, (req, res) => {
Transducer.findById(req.params.id, (err, foundTransducer) => {
if(err){
res.redirect('/en/dashboard');
} else {
res.render('dashboard/transducer_edit-dashboard', {transducer: foundTransducer});
}
});
};
我做错了什么?请帮忙......
答案 0 :(得分:0)
抱歉,我刚刚解决了。
我在我的路线中打电话给中间件:
router.get('/ en / dashboard / products / transducers /:id / edit',middleware.isLoggedIn,transducer_controller.get_transducer_edit);
我试图在我的控制器中调用它,如:
exports.get_transducer_edit = middleware.isLoggedIn,(req,res)=> { ...
错!
再次感谢。