-1

Problem in accessing flash messages on view in node.js In my Controller

this.req.flash('info','successfully submited');
this.redirect("/home");

In my home view I am not able to get flash messages as

req.flash('info');

EDIT In controller

self.req.flash('message','hello');

In view

<%= req.flash('message) %>

In server.js

app.configure(function (){

  app.use(express.cookieParser());
  app.use(express.session({ secret:'yoursecret',cookie: { maxAge: 24 * 60 * 60 * 1000 }}));
  app.use(passport.initialize());
  app.use(locomotive.session());
  app.use(flash());
  app.use(passport.session());
  app.use(app.router);

  app.dynamicHelpers({ messages: require('express-messages') });
});

I have the locomotive framework.

Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
user3680001
  • 91
  • 1
  • 7
  • There isn't enough information in your question to help. Are you correctly requiring `connect-flash`? Did you use it with `app.use(flash)`? What else have you tried? – Matthew Bakaitis May 28 '14 at 13:17
  • yes i have added app.use(flash) in server.js and also used connect-flash. problem comes in view when i use flash().error comes that undefined method flash(). – user3680001 May 28 '14 at 13:23
  • 1
    Can you post that code? If that's the problem and it isn't in the question, there's not much we can do to help. :) – Matthew Bakaitis May 28 '14 at 13:24

2 Answers2

0

Please see tempdata example https://github.com/MKxDev/TempData

var tempData = require('tempdata');
    var app = express();

    app.configure(function() {
        ...

        // This has to appear BEFORE the router
        app.use(express.cookieParser());
        app.use(express.session({ secret: 'your_super_secret_session_key' })); // please change this!
        app.use(tempData);

        ...
    });

    ...

    // Routes
    app.get('/', function(req, res) {
        // Retrieve tempData value here. It won't exist unless the request
        // was redirected
        var tempVal = JSON.stringify(req.tempData.get('test_val'));

        res.render('index', { title: 'Express', temp: tempVal });
    });

    app.post('/', function(req, res) {
      // Set tempData value here
        req.tempData.set('test_val', { x: 'Hello World!' });

        res.redirect('/');
    });
0

Move your app.use(flash()) higher in the order...see below. Flash needs to be initialized before passport so that flash is recognized and available to passport.

app.configure(function (){
  app.use(express.cookieParser());
  app.use(express.session({ secret:'yoursecret',cookie: { maxAge: 24 * 60 * 60 * 1000 }}));
  app.use(flash()); // moved this up a few lines
  app.use(passport.initialize());
  app.use(locomotive.session());
  app.use(passport.session());
  app.use(app.router);
  app.dynamicHelpers({ messages: require('express-messages') });
});
Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
  • sir,still error comes in view that undefined flash() method – user3680001 May 28 '14 at 13:53
  • In the view? Ahhh... First, is there a typo in your view? There's a typo in the question regarding the view that I didn't notice - a missing single-quote. Second, how are you passing `req.flash` to the view? Are you setting `self = this` at some point? – Matthew Bakaitis May 28 '14 at 14:03
  • Matt Bakaitis yes i m setting self=this and req.flash – user3680001 Jun 20 '14 at 06:27