Abubakr Elghazawy
Abubakr Elghazawy

Reputation: 985

Cannot set property 'messages' of undefined

I'm still learning nodejs by building asmall blog app system but when i start npm that error appear in browser window heres my package.json:

{
  "name": "nodeblog",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.17.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.3",
    "express": "~4.15.2",
    "jade": "~1.11.0",
    "morgan": "~1.8.1",
    "serve-favicon": "~2.4.2",
    "mongodb": "*",
    "monk": "*",
    "connect-flash": "*",
    "express-validator": "*",
    "express-session": "*",
    "express-messages": "*",
    "multer": "*",
    "moment": "*"
  }
}

and app.js:

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

here is the error message:

TypeError: Cannot set property 'messages' of undefined
    at C:\NodeJS\NodeBlog\app.js:73:21
    at Layer.handle [as handle_request] 
(C:\NodeJS\NodeBlog\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix 
(C:\NodeJS\NodeBlog\node_modules\express\lib\router\index.js:317:13)
    at 
C:\NodeJS\NodeBlog\node_modules\express\lib\router\index.js:284:7
    at Function.process_params 
(C:\NodeJS\NodeBlog\node_modules\express\lib\router\index.js:335:12)
    at next 
(C:\NodeJS\NodeBlog\node_modules\express\lib\router\index.js:275:10)
    at C:\NodeJS\NodeBlog\node_modules\connect-flash\lib\flash.js:21:5
    at Layer.handle [as handle_request] 
(C:\NodeJS\NodeBlog\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix 
(C:\NodeJS\NodeBlog\node_modules\express\lib\router\index.js:317:13)
    at C:\NodeJS\NodeBlog\node_modules\express\lib\router\index.js:284:7

Upvotes: 1

Views: 2855

Answers (1)

Amberlamps
Amberlamps

Reputation: 40478

It is res.locals, not req.locals.

Upvotes: 6

Related Questions