Shankar Ghimire
Shankar Ghimire

Reputation: 146

'messages is not a function' nodeJs

messages is not a function

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)

Upvotes: 0

Views: 1815

Answers (3)

Quintin Henn
Quintin Henn

Reputation: 91

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
}));

Upvotes: 0

Kestone Dennis
Kestone Dennis

Reputation: 25

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(); });

Upvotes: 0

Yaco
Yaco

Reputation: 41

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();
});

Upvotes: 4

Related Questions