req.flash() requires sessions

夙愿已清 提交于 2020-01-04 04:04:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!