0

messages is not a function

  • Why != messages('message',locals) is showing me error
  • even though it is right

Please help me to find error. thank you

app.js

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

users.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('/');
    }
});
});

message.pug

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

layout.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')

error

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)
Shankar Ghimire
  • 138
  • 4
  • 15
  • Try `!{messages('message',locals)}` – Şivā SankĂr Feb 04 '19 at 03:51
  • sorry its not working `> 23| !{messages('message',locals)} ------------^` and error is `unexpected text "!{mes" ` – Shankar Ghimire Feb 04 '19 at 04:41
  • post your `package.json` – Şivā SankĂr Feb 04 '19 at 05:01
  • package.json `{ "name": "crudapp", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "body-parser": "^1.18.3", "connect-flash": "^0.1.1", "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", "express-messages": "^1.0.1", "express-session": "^1.15.6", "express-validator": "^5.3.1", "http-errors": "~1.6.2", "mongoose": "^5.4.7", "morgan": "~1.9.0", "pug": "2.0.0-beta11" } }` – Shankar Ghimire Feb 04 '19 at 06:45
  • Router included below `app.use(function (req, res, next) { res.locals.messages = require('express-messages')(req, res); next(); });` in app.js ? – Şivā SankĂr Feb 04 '19 at 07:21
  • yes i have included that in app.js – Shankar Ghimire Feb 04 '19 at 09:23
  • I have replicated your code, The code which you have posted here is working fine. Something is wrong with the code that not posted here. – Şivā SankĂr Feb 04 '19 at 09:54

3 Answers3

4

I don' know if you already did, but make sure that the following declaration is before app.use(app,router) or any route use:

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

Ensure that the module installed is express-messages not express message

In the app.js have the following code:

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

0

In addition to the answer from Yaco

Also make sure you have express-session installed.

Placing the configuration of express-messages before declaring my routes,
I get the error: req.flash() requires sessions

A configuration like this might fix the error for you:

const session = require('express-session');
app.use(session({
  secret: 'secret',
  resave: false,
  saveUninitialized: true
}));
Quintin Henn
  • 91
  • 1
  • 6