-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.

3
  • 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? Commented May 28, 2014 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(). Commented May 28, 2014 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. :) Commented May 28, 2014 at 13:24

2 Answers 2

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('/');
    });
Sign up to request clarification or add additional context in comments.

Comments

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') });
});

3 Comments

sir,still error comes in view that undefined flash() method
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?
Matt Bakaitis yes i m setting self=this and req.flash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.