0

I'm hoping someone can help with showing flash messages in Express via a Handlebars view (which uses the bootstrap markup).

In app.js I have the below modules and middleware to try and get flashes working

//require modules
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const hbs = require('express-handlebars');
const session = require('express-session');
const flash = require('connect-flash');
const routes = require('./routes/index');
const app = express();

// view engine setup
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout',layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(express.static(path.join(__dirname, 'public')));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(expressValidator());

app.use(cookieParser());

app.use(session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

app.use(flash());

app.use((req, res, next) => {
res.locals.h = helpers;
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
next();
});

app.use('/', routes);

module.exports = app;

and a route router.post('/store/add', storeController.createStore);

which has the controller function

exports.createStore = async (req, res) => {
  const store = new Store(req.body);
  await store.save();
  req.flash('error', 'leave a review');
  console.log('my-messages',req.flash());
  res.redirect('/');    
};

when I create a new store and am redirected to homepage the console.log shows the correct value my-messages { error: [ 'leave a review' ] } but I cannot get it into the view

my homepage ('/') view is

<h1>{{title}}</h1>
  <p>Hi! Welcome to {{title}} </p>
  <p>This page was built by {{created}}</p>

 {{#if message}}
<div class="alert alert-danger">{{message}}</div>
{{/if}}

{{#if errors}}
    {{#each errors}}
        <div class="error">
            {{msg}}
        </div>
    {{/each}}
{{/if}}

but nothing shows up. I've read quite a few similar questions on SO, but can't seem to get this right.

Any help much appreciated.

Stuart Brown
  • 977
  • 2
  • 22
  • 47
  • Possible duplicate of [How to send flash messages in Express 4.0?](https://stackoverflow.com/questions/23160743/how-to-send-flash-messages-in-express-4-0) – marekful Jan 27 '18 at 21:44
  • Try `res.locals.message = '...'` in the controller. Also you should probably try more [research](https://google.com?q=req.flash+site%3Astackoverflow.com) before asking. – marekful Jan 27 '18 at 21:47
  • thanks @marekful I have looked at that answer and the gists mentioned in the answers. I'm trying to use `connect-flash` rather than `express-flash` which is used in https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423 and the other answer (and the gist https://gist.github.com/raddeus/11061808) doesn't use handlebars – Stuart Brown Jan 27 '18 at 22:12

1 Answers1

1

OK, so this is how I've worked things based on https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423 from this https://stackoverflow.com/a/28221732/1699434 answer.

After app.use(flash()) in app.js I added:

app.use(function(req, res, next){
// if there's a flash message in the session request, make it available 
in the response, then delete it
  res.locals.sessionFlash = req.session.sessionFlash;
  delete req.session.sessionFlash;
  next();
});

In my routes file (index.js) I added the example in the gist:

router.all('/session-flash', function( req, res ) {
  req.session.sessionFlash = {
    type: 'info',
    message: 'This is a flash message using custom middleware and express-session.'
  }
  res.redirect(301, '/');
});

Then I created a handlebars partial message.hbs (which makes use fo the contains helper from npmjs.com/package/handlebars-helpers:

{{#if sessionFlash.message}}
  <div id="flash-messages" class="container">
      {{#contains sessionFlash.type "info"}}
      <div class="alert alert-info">
            {{{sessionFlash.message}}}
      </div>
      {{/contains}}
      {{#contains sessionFlash.type "success"}}
      <div class="alert alert-success">
              {{{sessionFlash.message}}}
      </div>
      {{/contains}}
      {{#contains sessionFlash.type "warning"}}
      <div class="alert alert-warning">
            {{{sessionFlash.message}}}
      </div>
       {{/contains}}
      {{#contains sessionFlash.type "error"}}
      <div class="alert alert-danger">
            {{{sessionFlash.message}}}
      </div>
      {{/contains}}
  </div>
{{/if}}

I can then include this in my other handlebars templates {{> message}}. This gives me flash messages carrying bootstrap styling.

Unfortunately I'm not able to send multiple flashes at the same time (either of the same or different types) but I think this is discussed in https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423 anyway as a limitation of the middleware approach. As I learn more maybe I'll address this but I don't have a use case for multiple flash messages at the moment anyway :)

Stuart Brown
  • 977
  • 2
  • 22
  • 47