问题
I have a problem with flash at views. I am using connect-flash.
there is my configuration
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('secret'));
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(function(req, res, next){
res.locals.flash = req.flash
next()
})
here I set flash message at controller action
exports.new = function(req, res){
req.flash('info', 'test')
res.render("session/new", {
title: 'Log In!'
})
}
and then, when I am trying to use flash('info') at view, I am getting this error
req.flash() requires sessions
I am new to nodejs, so excuse me please if it's stupid question.
回答1:
maybe you called req.session.destroy() before the flash-data is passed to res.locals?!
回答2:
Take a look at this link. It seems you have to specify it when you're configuring your express server.
回答3:
Try the Redirect instead of render.
Ex.
res.redirect('session/new');
来源:https://stackoverflow.com/questions/19168355/req-flash-requires-sessions