title: Flash Message from ejs in Express 4
body: <p>I am trying to show a confirmation message before user deletes anything. I've tried following various related sources that I found in internet, but couldn't get them to work. I am using EJS template and Express 4.</p>
  
  <p>app.js</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  
  var routes = require('./routes/index');
  var users = require('./routes/users');
  
  var app = express();
  var flash = require('connect-flash');
  var session = require('express-session');
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  
  app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
  app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap
  
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(__dirname + '/public/favicon.ico'));
  app.use(logger('dev'));
  app.use(cookieParser());
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(require('stylus').middleware(path.join(__dirname, 'public')));
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use(session({ secret: 'zxcv' })); // session secret
  app.use(flash()); // use connect-flash for flash messages stored in session
  
  app.use('/', routes);
  app.use('/users', users);
  
  // catch 404 and forward to error handler
  app.use(function (req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
  });
  
  // error handlers
  
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
      app.use(function (err, req, res, next) {
          res.status(err.status || 500);
          res.render('error', {
              message: err.message,
              error: err
          });
      });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function (err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
          message: err.message,
          error: {}
      });
  });
  
  app.listen(8000);
  module.exports = app;
  </code></pre>
  
  <p>index.js</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  var searchModule = require('../crud_module/search.js');
  var updateModule = require('../crud_module/update.js');
  var deleteModule = require('../crud_module/delete.js');
  
  
  /* GET home page. */
  router.get('/', function (req, res) {
      res.render('index', { title: '' });
  });
  
  router.post('/search-results', function (req, res) {
      searchModule.search(req.body, function (data) {
          res.render('index', { title: '', results: data });
      });
  });
  
  router.post('/edit-data', function (req, res) {
      searchModule.searchById(req.body, function (data) {
          res.render('edit', { title: '', results: data });
      });
  });
  
  router.post('/save-changes', function (req, res) {
      if (req.body.change == "update") {
          updateModule.saveChanges(req.body, function (err) {
              if (err) {
                  res.render('edit', { message: req.flash('Error', 'Error Occured') });
  
              } else {
                  //req.flash('success', 'Your name was updated');
                  res.render('edit', { message: req.flash('success', 'Data was updated')) });
              }
              res.redirect('/edit-data');
          });
      }
  if (req.body.change == "delete") {
          deleteModule.deleteRecord(req.body, function (data) {
              res.render('edit', { title: 'Volume Tracker', confirmation: data });
  
          });
      }
  });
  
  
  module.exports = router;
  </code></pre>
  
  <p>edit.ejs</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"&gt;
      &lt;link href="node_modules/popups/css/popupS.min.css" rel="stylesheet" /&gt;
      &lt;script type="text/javascript" src="https://gc.kis.scr.kaspersky-labs.com/5B1FA715-F8FF-784F-A4C9-0D867337CB3D/main.js" charset="UTF-8"&gt;&lt;/script&gt;&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;script src="node_modules/elasticsearch/src/elasticsearch.angular.js"&gt;&lt;/script&gt;
      &lt;script src="node_modules/popups/dist/popupS.min.js"&gt;&lt;/script&gt;
      &lt;script language="javascript"&gt;
  
      &lt;/script&gt;
  &lt;style type="text/css"&gt;
  
  
  &lt;/style&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;br&gt;
  
  &lt;form action='/save-changes' method='post'&gt;
  &lt;div class="container"&gt;
  
  
  
      &lt;% if(locals.results) { %&gt;
        &lt;table class="table table-bordered table-hover table-condensed"&gt; 
          &lt;tr&gt;
              &lt;th  bgcolor="silver"&gt;Term Key&lt;/th&gt;
              &lt;td&gt;
                &lt;input name="termKey"  value= "&lt;%= results[0]._source.termkey %&gt;" style="border:none; width:100%"/&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
  
            &lt;tr&gt;
              &lt;th bgcolor="silver"&gt;Active&lt;/th&gt;
              &lt;td&gt;
                &lt;input name="active" value= "&lt;%= results[0]._source.active %&gt;" style="border:none; width:100%"/&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
  
            &lt;tr&gt;
              &lt;th bgcolor="silver"&gt;Term Description&lt;/th&gt;
              &lt;td&gt;
                &lt;input name="termDescription" value= "&lt;%= results[0]._source.termdescription %&gt;" style="border:none; width:100%"/&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
  
  
        &lt;/table&gt;  
        &lt;div align="center"&gt;
        &lt;button type="submit" class="btn btn-info" name="change" value="update"&gt; Save Changes &lt;/button&gt;
        &lt;button type="submit" class="btn btn-danger" name="change" value="delete"&gt; Delete Record &lt;/button&gt;
        &lt;/div&gt;
  
  
      &lt;% } %&gt;
      &lt;/form&gt;
      &lt;br&gt;
  
      &lt;/div&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Here, when the user will click on the delete button, a confirmation message will be shown (here--> in index.js, I first tried showing confirmation message after user updates anything; which didn't work either). But whenever I try to include this: </p>
  
  <pre><code>    &lt;% if (message.length &gt; 0) { %&gt;
              &lt;div class="alert alert-danger"&gt;&lt;%= message %&gt;&lt;/div&gt;
          &lt;% } %&gt;
  in edit.ejs , I get error:
  ReferenceError: 
      92|     &lt;/form&gt;
      93|     &lt;br&gt;
   &gt;&gt; 94|     &lt;% if (message.length &gt; 0) { %&gt;
      95|         &lt;div class="alert alert-danger"&gt;&lt;%= message %&gt;&lt;/div&gt;
      96|     &lt;% } %&gt;
  
  
  
  
   message is not defined
          at eval (eval at &lt;anonymous&gt; (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\ejs\lib\ejs.js:495:12), &lt;anonymous&gt;:41:12)
      at Template.compile.returnedFn (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\ejs\lib\ejs.js:524:17)
      at View.exports.renderFile [as engine] (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\ejs\lib\ejs.js:378:31)
      at View.render (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\express\lib\view.js:76:8)
      at Function.app.render (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\express\lib\application.js:527:10)
      at ServerResponse.res.render (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\express\lib\response.js:900:7)
      at \ElasticSearch\ES\VolumeTracker\VolumeTracker\routes\index.js:22:13
      at \ElasticSearch\ES\VolumeTracker\VolumeTracker\crud_module\search.js:45:9
      at tryCallOne (\ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\elasticsearch\node_modules\promise\lib\core.js:37:12)
      at \ElasticSearch\ES\VolumeTracker\VolumeTracker\node_modules\elasticsearch\node_modules\promise\lib\core.js:123:15
  </code></pre>