Search code examples
node.jsexpressconnect-flash

Node - connect-flash not working on redirect


I'm running into some weird issues with connect-flash, I've used it in other projects in the exact same manner and it worked fine, here is what I have:

Some Route

if (err) {
    req.flash('message', [{
      class: 'alert-danger',
      message: 'TEST'
    }]);

    res.redirect('/error');
}

error route

router.get('/error', function (req, res, next) {
  console.log('---in error route')
  console.log(req.flash('message'));

 res.render('error', {
    message: req.flash('message'),
    layout: layout
  });
})

console.log

---in error route
[ { class: 'alert-danger', message: 'TEST' } ]

error.hbs

{{#if message}} 
    {{#each message}}
        <div class="{{this.class}}">{{this.message}}</div>
    {{/each}} 
{{/if}}

Clearly it's in there, but there is no printout, if I copy the exact smae thing from the first route to error, it displays (which won't work for me, but just for testing to make sure the hbs template logic works):

router.get('/error', function (req, res, next) {

  req.flash('message', [{
    class: 'alert-danger',
    message: 'TEST'
  }]);
  res.render('error', {
    message: req.flash('message'),
    layout: layout
  });
})

It's probably something simple, but I have no idea what it is.


Solution

  • Ok after some experimentation and by pure accident, I found out that The console.log apparently clears the flash!!!!

    I hope this helps anyone who runs into this issue.