1

I am trying to show message in handlebar template using connect-flash package but message is not showing and its print count when i use {{loginMessage}} in login page. but not display message. here is my code

file..routes/index.js

 var express =  require('express');
 var router =   express.Router();
 var mongoose = require('mongoose');
 var flash =    require('connect-flash');
 const m_db =   require('.././config/mongo_connection');
 var Site =     require('.././models/site');
 var User = require('../models/user');
 var bcrypt   = require('bcrypt-nodejs');
 var passport = require('passport');
 var LocalStrategy = require('passport-local').Strategy;
 require('.././config/passport')(passport);
 //routes start here
 router.get('/login', function(req, res, next) {    
 res.render('passports/login.hbs', { message:   req.flash('loginMessage','failed login') });

})

my app.js

  app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: false
}));
  app.use(flash());
 //
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(express.static(path.join(__dirname, 'public')));
 // passport config

 app.use(logger('dev'));
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));

 app.use('/', index);
 app.use('/users', users);
 app.use('/api', api);

 // 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 handler
  app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
 });
  module.exports = app;

Please help! Thanks!

4

1 回答 1

0
router.get('/login', function(req, res, next) {           

  res.render('passports/login.hbs', { message: req.flash('loginMessage') }); 
     .
     .

我想说的是:

req.flash('loginMessage','failed login')  

用于将 flash 变量名称设置loginMessagefailed login

req.flash('loginMessage')

这是您稍后阅读您设置的闪光灯的时候。上面的代码返回闪存消息数组(如果设置了多个闪存)loginMessage

于 2017-03-20T09:42:25.227 回答