jaredhanson / connect-flash

Flash message middleware for Connect and Express.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: req.flash() requires sessions

lbois opened this issue · comments

Hello

I get the error "Error: req.flash() requires sessions" using connect-flash, I use Express 4.17... I do not understand it seems my setup is correct.
Here is my main.js

const Session = require('express-session');
const redis = require('redis');

let RedisStore = require('connect-redis')(Session)
let redisClient = redis.createClient()

app.use(flash());
app.use(Session({
    store: new RedisStore({ client: redisClient }),
    secret: config.server.secret,
    cookie: {maxAge: Date().now + (60 * 1000 * 30) } ,
    resave: false, 
    saveUninitialized: false
}));

Here is the code of the route where the error come from

const express = require('express'),
    router = express.Router(),
    passport = require('passport');
 
router.get('/',
    require('connect-ensure-login').ensureLoggedOut(),
    (req, res) => {
        res.render('register', {
            user : null,
            errors : {
                username : req.flash('username'), <-- error here
                email : req.flash('email')
            }
        });
    });

Redis is started up

Thanks for your help

Laurent

I fixed the problem.
Router after flash.
Please close this issue

commented

@lbois , Please note you may close it by your self.