Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: req.flash() requires sessions

I'm new to node and I'm pretty sure I've set up the middle ware and express to use flash messaging however I still get the error:

Error: req.flash() requires sessions

Setup

//express.js
     var flash = require('connect-flash')

     module.exports = function (app, config, passport) {
         app.use(flash());
     };

//route js
     exports.loginGet = function (req, res) {
       res.render('users/login', {
         title: 'Login',
         message: req.flash('error') //error in question
       });
     };

What else can I do to make sure I have everything set up correctly and get it working?

like image 795
Jamie Hutber Avatar asked May 31 '15 22:05

Jamie Hutber


People also ask

How do I show flash messages in node JS?

To implement flash messages in NodeJs with connect-flash module, you need to install the required dependencies using the command. Express: Required by the library connect-flash to run. Express-session: to create a session whenever a message is flashed, so that the user can be redirected to a particular page.

What is connect-flash?

Connect-flash module for Node. js allows the developers to send a message whenever a user is redirecting to a specified web-page. For example, whenever, a user successfully logged in to his/her account, a message is flashed(displayed) indicating his/her success in the authentication.

What is Express flash in node JS?

Flash Messages for your Express Application. Flash is an extension of connect-flash with the ability to define a flash message and render it without redirecting the request.


2 Answers

In my case the issue was that Redis was not listening. I found that out by enabling the logErrors property:

new RedisStore({
  host: 'localhost',
  port: '6379',
  logErrors: true,
});

Which resulted in messages like these:

Warning: connect-redis reported a client error: Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
like image 141
beeman Avatar answered Sep 16 '22 15:09

beeman


i was having these issues and I solve them by respecting the cascading

app.use(passport.initialize());
app.use(passport.session());
//SESSION FLASH
app.use(flash());
like image 28
Ayoub Aarab Avatar answered Sep 16 '22 15:09

Ayoub Aarab