0

I'm wondering why my connect-flash is not working. I know connect-flash try to save message into session. So I declare session like this :

app.use(session());

Then I set up flash :

app.use(flash());

The problem is that when I using with connect-flash, it didn't show just set message after redirect action.

req.flash('flash_var', 'ok');
res.redirect('/mail/send');

I couldn't see any message at the '/mail/send' routers.

app.get('/mail/send', function(req, res) {
  res.render('mail/console', {
    console.log("flash : "+req.flash('flash_var'); // null
    message: req.flash('flash_var')
  });
});

The console message are printing null values, The previous step does not seem to send the value normally. Of course I didn't see any 'flash_var' at ejs page.

What's wrong with my code?

if(req.flash("flash_var") != ""){
  flash_var = req.flash("flash_var");
  console.dir("flash_var dir : "+req.flash("flash_var"));
}
Richard
  • 351
  • 4
  • 17

1 Answers1

0

You don't use req.flash('flash_name') to get a flash message back. What you would do is req.flash()['flash_name']. In your case the message value in your res.render options object would be req.flash()['flash_var']. req.flash() is not an individual getter, it returns an object containing all flashes on the request object. The other way to use req.flash() is with two string inputs, like you did with req.flash('flash_var', 'ok').

dkimot
  • 2,239
  • 4
  • 17
  • 25