Flash消息显示为“ 1”,不显示任何Flash消息

时间:2019-06-22 19:38:27

标签: node.js ejs connect-flash

我在Flash消息中遇到另一个问题,那就是每当我打开带有Flash消息的登录页面时,它就会显示数字“ 1”。我不知道这是什么意思。


app.js

const express = require('express')
const mongoose = require('mongoose')
const session = require('express-session')
const expressValidator = require('express-validator')
const MongoStore = require('connect-mongo')(session)
const path = require('path')
const passport = require('passport')
const bodyParser = require('body-parser')
const flash = require('connect-flash')
const routes = require('./routes/index')
const errorHandlers = require('./handlers/errorHandlers')
require('./handlers/passport')

const app = express();

app.use(express.static(path.join(__dirname + '/public')))
app.set('views', path.join(__dirname, 'public'))
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'ejs')



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

app.use(expressValidator());

// populates req.cookies with any cookies that came along with the request
//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(passport.initialize());
app.use(passport.session());


app.use(flash());

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


// After allllll that above middleware, we finally handle our own routes!
app.use('/', routes);


// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);

// One of our error handlers will see if these errors are just validation errors
//app.use(errorHandlers.flashValidationErrors);


// done! we export it so we can start the site in start.js
module.exports = app;



userController.js

const mongoose = require('mongoose')
const User = mongoose.model('User')
const promisify = require('es6-promisify')
const { check } = require('express-validator/check')

exports.loginForm = (req, res) => {

  res.render('login',{
  msg: req.flash('error','welcome in login page')})
  ;


};

exports.registerForm = (req, res) => {
  res.render('signup')
};

exports.aboutUs = (req, res) => {
  res.render('about', { title: 'Register' })
};

exports.dash = (req, res) => {
  res.render('dashhome', { title: 'Register' })
}


exports.homePage = (req, res) => {
  res.render('index')
}

exports.contactForm = (req, res) => {
  res.render('contact')
}}


login.ejs

This is my main syntax of ejs for flash message

    <% if (msg) { %>
    <%=msg %>
    <% } %>     

我希望结果是“欢迎登录页面”,但我不知道打开登录页面并看到此结果为“ 1”而不是Flash消息时如何得到此输出。

2 个答案:

答案 0 :(得分:0)

您应在处理响应之前将数据分配给req.flash,然后在呈现视图时可以传递分配的数据的实例。

exports.loginForm = (req, res) => {
    req.flash('error', 'welcome in login page');
    res.render('login',{
         msg: req.flash('error')
    })
};

答案 1 :(得分:0)

渲染视图之前,您只需要使用Flash来保存本地消息,如下所示:

request.flash("error","Error Occured");
response.locals.messages = request.flash();
res.render('login',{
     msg: req.flash('error')
})

一旦设置了本地消息,便可以获取它。在这里,消息是主要对象,它的关键称为错误。因此,在视图中,您可以像这样访问它:

<% if (locals.messages) { %>

    <script language="javascript">
       alert("<%= messages.error %>");
    </script>

<% } %>

您可以在if条件下执行任意操作。