Flash消息未显示,但其余代码可以正常工作

时间:2019-04-18 19:23:47

标签: node.js express npm connect-flash

为什么即使我的代码也无法正常显示Flash消息。当我输入所有详细信息但Flash消息不显示时,它将重定向到其他页面。每当我输入我的详细信息时,它都会反映到“ /”页面,但不显示任何即时消息。请有人帮我。我有一些解决方案,但是我仍然不明白,当我使用这种类型的语法“ <%= mess%>”时,这与html有关,第一个开括号总是显示红色,并说这是没有定义。

index.html

<html>
<head>
    <title>form-page</title>
</head>
<body>

    <form action="/register" method="POST">
    <h3>Name</h3>
    <input type="text" name="name">
    <br>
    <h3>Email</h3>
    <input type="email" name="email">
    <br>
    <h3>Password</h3>
    <input type="password" name="password">
    <input type="submit" value="click">



</form>
</body>
</html>

这里所有功能都是为了userController.js

const mongoose = require('mongoose');
const User = mongoose.model('User');
const promisify = require('es6-promisify');
//exports.registerForm = (req, res) => {
  //  res.render('index', { title: 'Register' });
  //};

  exports.register = async (req, res, next) => {
    const user = new User({ email: req.body.email, name: req.body.name });
    const register = promisify(User.register, User);
    await register(user, req.body.password);
    //const newBookmark = await user.save();
    //res.send("it works")
    req.flash('mess', `whoooooo positive news`)
    res.redirect('/')
    next();
  };

exports.getUsers = async (res, req) => {

  const storeUser = await User.find();
  console.log(storeUser);
  res.render('store',{title: 'store'}) 
}

app.js

const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session')

const MongoStore = require('connect-mongo')(session);
const path = require('path');
const bodyParser = require('body-parser');
const flash = require('connect-flash');
const routes = require('./routes/index');
const errorHandlers = require('./handlers/errorHandlers');

const app = express();

app.use(express.static(path.join(__dirname + '/views')))

app.set('views', path.join(__dirname, 'public'))
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')




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


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('/', routes);


app.use(errorHandlers.notFound);
module.exports = app;

index.js

const express = require('express');
const router = express.Router();
//const storeController = require('../controllers/storeController');
const userController = require('../controllers/userController');
const authController = require('../controllers/authController');
const { catchErrors } = require('../handlers/errorHandlers');

router.get('/', (req, res) => {
  res.render('index', { messages: req.flash('mess') });
});

router.get('/register', (req, res) => {
  res.render('index')
});

router.post('/register',
  userController.register
);

router.get('/userlist', userController.getUsers)
module.exports = router;

1 个答案:

答案 0 :(得分:0)

您将mess的Flash数据呈现为messages,而不是mess。因此,您可以通过<%= messages %>的方式显示所需的数据。
请注意,消息是一个数组,因此应检查数组是否为空。

<% if ( messages.length > 0 ) { %>
  <p><%= messages %> </p>
<% } %>