我仍然通过构建asmall博客应用系统来学习nodejs,但是当我开始使用npm时
该错误出现在浏览器窗口中
继承我的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": "*"
}
}
和app.js
:
// connect-flash
app.use(flash());
app.use(function(req, res,next){
req.locals.messages =require('express-messages')(req,res);
next();
});
这是错误消息:
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
答案 0 :(得分:3)
是res.locals
,而不是req.locals
。