我使用的是express 4、passport 和express-flash。当我使用罐装护照中间件功能并设置 failureFlash: true 时,一切正常。但是,当我在同一路由文件的注册函数中使用自定义回调时,它不起作用。messages.info 对象为空。
这工作正常:
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/dashboard',
failureRedirect: '/login',
failureFlash: true
}),
function(req, res){
}
);
Run Code Online (Sandbox Code Playgroud)
这导致我的 messages.info 对象为空:
router.post('/register', function(req, res, next){
passport.authenticate('local-signup', function(err, user, info){
if(err){
req.flash('info', err);
res.render('register');
}else{
res.render('profile');
}
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
我使用 jade 作为我的预处理器:
if (messages.info)
.message.info
span= messages.info
Run Code Online (Sandbox Code Playgroud)
不劳而获!
好问题。
我发现一些工作代码可以发送如下所示的闪存消息:
router.use(function(req, res, next){
res.locals.errors = req.flash("error");
next();
})
Run Code Online (Sandbox Code Playgroud)
在EJS中
<% errors.forEach(function(error) { %>
<div class="alert alert-danger" role = "alert">
<%= error %>
</div>
<% }) %>
Run Code Online (Sandbox Code Playgroud)
当地人资源
包含作用域为请求的响应局部变量的对象,因此仅可用于在该请求/响应周期(如果有)期间呈现的视图。否则,此属性与 app.locals 相同。
此属性对于公开请求级信息非常有用,例如请求路径名称、经过身份验证的用户、用户设置等。
归档时间: |
|
查看次数: |
4934 次 |
最近记录: |