0

消息不是函数

  • 为什么!= messages('message',locals)显示我错误
  • 即使是对的

请帮我找出错误。谢谢你

应用程序.js

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

用户.js

router.post('/POST', function(req, res){
    user = new user();
    user.firstName = req.body.first;
    user.LastName = req.body.last;
    user.email = req.body.email;
    user.password = req.body.password;
user.save(function (err) {
    if(err){
        console.log(err);
    }else{
        req.flash('success', 'You have been registered');
        res.redirect('/');
    }
});
});

消息.pug

.messages
    each type in Object.keys(messages)
        each message in messages[type]
            div(class="alert alert-"+type) #{message}

布局.pug

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/bower_components/bootstrap/dist/css/bootstrap.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    nav.navbar.navbar-expand-md.navbar-dark.bg-dark.mb-4
      button.navbar-toggler(type='button', data-toggle='collapse', data-target='#navbarCollapse', aria-controls='navbarCollapse', aria-expanded='false', aria-label='Toggle navigation')
        span.navbar-toggler-icon
      #navbarCollapse.collapse.navbar-collapse
        ul.navbar-nav.mr-auto
          li.nav-item
            a.nav-link(href='/') Home
              span.sr-only (current)
          li.nav-item
            a.nav-link(href='/users') Register
          li.nav-item
            a.nav-link(href='#') Disabled
        form.form-inline.mt-2.mt-md-0(method='post' action='')
          input.form-control.mr-sm-2(type='text', placeholder='Search', aria-label='Search')
          button.btn.btn-outline-success.my-2.my-sm-0(type='submit') Search
    != messages('message',locals)
    block content
    br
    hr
    footer
      p Copyright © 2019
        script(src='/bower_components/jquery/dist/jquery.js')
        script(src='/bower_components/bootstrap/dist/js/bootstrap.js')

错误

TypeError: C:\Users\User\WebstormProjects\crudApp\views\layout.pug:23
    21|           input.form-control.mr-sm-2(type='text', placeholder='Search', aria-label='Search')
    22|           button.btn.btn-outline-success.my-2.my-sm-0(type='submit') Search
  > 23|     != messages('message',locals)
    24|     block content
    25|     br
    26|     hr

messages is not a function
    at eval (eval at wrap (C:\Users\User\WebstormProjects\crudApp\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:60:46)
    at template (eval at wrap (C:\Users\User\WebstormProjects\crudApp\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:87:239)
    at Object.exports.renderFile (C:\Users\User\WebstormProjects\crudApp\node_modules\pug\lib\index.js:418:38)
    at Object.exports.renderFile (C:\Users\User\WebstormProjects\crudApp\node_modules\pug\lib\index.js:408:21)
    at View.exports.__express [as engine] (C:\Users\User\WebstormProjects\crudApp\node_modules\pug\lib\index.js:455:11)
    at View.render (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\response.js:1008:7)
    at C:\Users\User\WebstormProjects\crudApp\app.js:94:7
    at Layer.handle_error (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\router\index.js:315:13)
    at C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle_error (C:\Users\User\WebstormProjects\crudApp\node_modules\express\lib\router\layer.js:67:12)
4

3 回答 3

4

我不知道您是否已经这样做了,但请确保以下声明在 app.use(app,router) 或任何路由使用之前:

app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});
于 2019-03-19T05:35:56.583 回答
0

确保安装的模块是 express-messages 而不是 express message

在 app.js 中有以下代码:

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

于 2020-07-11T11:40:39.747 回答
0

除了Yaco的回答

还要确保您安装了express-session

在声明我的路线之前放置 express-messages 的配置,
我收到错误:req.flash() requires sessions

像这样的配置可能会为您修复错误:

const session = require('express-session');
app.use(session({
  secret: 'secret',
  resave: false,
  saveUninitialized: true
}));
于 2021-12-22T17:52:57.967 回答