错误:“消息不是函数”-req.flash()

时间:2018-11-16 22:27:47

标签: node.js mongodb express ejs

我有req.flash()设置,并且每当我注销时都希望它显示一条消息,但是我收到一条错误消息,而不是来自我的欢迎页面(我重定向到的页面,错误:消息不是功能)。我不明白的是我的请求访问了views / message.ejs IS ,我什至还可以console.log(message)。

Message.ejs

<div id="messages">
<% Object.keys(messages).forEach(function (type) { %>
  <% messages[type].forEach(function (message) { %>
    <%= message %>
  <% }) %>
<% }) %>
</div>

登出路线

app.get('/logout', function(req, res){
    req.logout();
    req.flash('success', 'You have been logged out.');
    res.redirect('/login');
});

Express Messages中间件

app.use(require('connect-flash')());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

欢迎页面

<% include ../partials/header-top.ejs %>
<title>Anon. - Welcome</title>
<% include ../partials/header-bottom.ejs %>

<div class="WATCH-OUT-card" id="content-warning">
    <center>
    <div class="card">
        <div class="card-header">
            Important
        </div>
        <div class="card-body">
            <h5 class="card-title">Watch Out!</h5>
            <p class="card-text">This site contains sensitive content.</p>
            <a href="/" class="btn btn-primary">Go to site.</a>
        </div>
    </div>
    </center>
</div>

<div id="welcomeContainer">
    <div class="signupbox">
        <div id="registerMessage">
            <%- messages('message', locals) %>
        </div>

        <h1 id = "SignupHeader">Sign Up Here</h1>

        <form action="/register" method="POST">
            <p class="signupText">Username</p>
            <input type="text" name="username" placeholder="Enter Username">    
            <p class="signupText">Password</p>
            <input type="password" name="password" placeholder="Enter Password">
            <p class="signupText">Confirm Pasword</p>
            <input type="password" name="password2" placeholder="Enter Password">
            <input type="submit" name="" value="Sign Up">
        </form> 
    </div>
    <p>Or</p>
    <div class="loginbox">
        <div id="registerMessage">
            <%- messages('message', locals) %>
        </div>
        <h1 id = "LoginHeader">Login Here</h1>
        <form action="/login" method="POST">
            <p class="LoginText">Username</p>
            <input type="text" name="username" placeholder="Enter Username">
            <p class="LoginText">Password</p>
            <input type="password" name="password" placeholder="Enter Password">
            <input type="submit" name="" value="Login">
        </form> 
    </div>
</div>

<% include ../partials/footer.ejs %>

错误:

TypeError: C:\Users\ericl\Documents\ANON\views\welcome.ejs:41
    39|     <div class="loginbox">
    40|         <div id="registerMessage">
 >> 41|             <%- messages('message', locals) %>
    42|         </div>
    43|         <h1 id = "LoginHeader">Login Here</h1>
    44|         <form action="/login" method="POST">

messages is not a function
    at eval (eval at compile (C:\Users\ericl\Documents\ANON\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:53:17)
    at returnedFn (C:\Users\ericl\Documents\ANON\node_modules\ejs\lib\ejs.js:653:17)
    at tryHandleCache (C:\Users\ericl\Documents\ANON\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\ericl\Documents\ANON\node_modules\ejs\lib\ejs.js:482:10)
    at View.render (C:\Users\ericl\Documents\ANON\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\ericl\Documents\ANON\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\ericl\Documents\ANON\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\ericl\Documents\ANON\node_modules\express\lib\response.js:1008:7)
    at C:\Users\ericl\Documents\ANON\routes\login.js:10:6
    at Layer.handle [as handle_request] (C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\ericl\Documents\ANON\node_modules\express\lib\router\index.js:275:10)

1 个答案:

答案 0 :(得分:0)

问题是我的欢迎文件中有2个消息功能。