好的,由于某种原因,会话闪存在我时可以工作res.render()
,但是当我尝试设置会话闪存然后重定向时,它不会显示。问题出在以下else
条款中的联系方式中)...
这是重定向期间记录到控制台的内容:
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{ info: [ 'Thanks we will return your message soon' ] }
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
如您所见,消息肯定在其中,但看起来它正在被一个空对象替换。
这是我的代码:
app.js
var flash = require('connect-flash');
// Grab sessions
var sessionFlash = function(req, res, next) {
res.locals.messages = req.flash();
console.log(res.locals.messages);
next();
}
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(viewHelpers());
app.use(express.bodyParser({ uploadDir : './' }));
app.use(expressValidator);
app.use(express.methodOverride());
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({
secret: config.secret,
cookie: {
maxAge: 365 * 24 * 60 * 60 * 1000
},
store: new MongoStore(config.db)
}));
app.use(flash());
app.use(sessionFlash);
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
views/layout.jade
- if (typeof message !== 'undefined')
=message
controllers/index.js
function controllers(params) {
controllers.contact = function(req, res) {
if (errors) {
// This works as intended
req.flash('info', 'Please fix the errors below.');
res.render('contact', {
title : 'Contact -',
message: req.flash('info'),
errors: errors,
params: params
});
return;
} else {
// This doesn't work
req.flash('info', 'Thanks we will return your message soon');
res.redirect('/');
}
};
return controllers
}
module.exports = controllers;