我正在关注Mozilla Express教程(https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/forms)并使用express-validator到达了部分。当我提交一个使用express-validator验证其内容的表单时,我不断收到错误“req.checkBody不是函数”(如果我删除生成该函数的行,我会收到其他错误,例如“req.sanitize not not功能“)。似乎没有正确识别express-validator。我正在使用node v6.3.1来获取它的价值。
已安装express-validator
├── async@2.5.0
├── body-parser@1.17.2
├── cookie-parser@1.4.3
├── debug@2.6.8
├── express@4.15.3
├── express-validator@3.2.0
├── moment@2.18.1
├── mongoose@4.11.0
├── morgan@1.8.2
├── nodemon@1.11.0
├── pug@2.0.0-rc.2
└── serve-favicon@2.4.3
我将其加载到主app文件(app.js)
中var expressValidator = require('express-validator');
并将其应用于
以下几行app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator()); // Add this after the bodyParser middlewares!
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
并尝试在另一个模块(genreController.js)中使用它
exports.genre_create_post = function(req, res, next) {
//Check that the name field is not empty
req.checkBody('name', 'Genre name required').notEmpty();
//Trim and escape the name field.
req.sanitize('name').escape();
req.sanitize('name').trim();
//Run the validators
var errors = req.validationErrors();
我哪里错了?我已安装并卸载它,我已从模块中删除了代码并重新输入,但无济于事。
req.checkBody is not a function
TypeError: req.checkBody is not a function
at exports.genre_create_post (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/controllers/genreController.js:48:9)
at Layer.handle [as handle_request] (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at /Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
at next (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3)
at router (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13)
at /Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
at next (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
at /Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:635:15
at next (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:260:14)
at Function.handle (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3)
at router (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13)
at /Users/cerulean/Documents/Projects/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7
答案 0 :(得分:2)
您的代码中使用require
的方式可能就是这样。由于你还没有插入代码的那部分,我假设有几个项目。无论如何,你可以查看一个有效的代码并弄清楚你做错了什么。或者,将整个代码粘贴到此处,我将能够为您提供帮助。
试试这个
// index.js
var util = require('util'),
logger = require('morgan'),
bodyParser = require('body-parser'),
cookieParser=require('cookie-parser'),
express = require('express'),
expressValidator = require('express-validator'),
app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(expressValidator()); // this line must be immediately after any of the bodyParser middlewares!
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));
app.get('/genres', require('./genre-controller.js').genre_create_post);
app.listen(8888);
和genre-controller.js
// genre-controller.js
exports.genre_create_post = function (req, res, next) {
console.log('my get method response');
//Check that the name field is not empty
req.checkBody('name', 'Genre name required').notEmpty();
//Trim and escape the name field.
req.sanitize('name').escape();
req.sanitize('name').trim();
//Run the validators
var errors = req.validationErrors();
res.end(errors);
};
package.json是
{
"name": "44836938-req-checkbody-is-not-a-function",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"body-parser": "^1.17.2",
"cookie-parser": "^1.4.3",
"express": "^4.15.3",
"express-validator": "^3.2.0",
"morgan": "^1.8.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
然后运行
$ npm install
$ node index.js
然后转到http://localhost:8888/genres
,您会看到验证工具正在运行..错误将是这样的
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:549:11)
at exports.genre_create_post (/home/projects/44836938-req-checkbody-is-not-a-function/genreController.js:15:9)
at Layer.handle [as handle_request] (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/layer.js:95:5)
at next (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/layer.js:95:5)
at /home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:335:12)
at next (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:275:10)
at cookieParser (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/cookie-parser/index.js:70:5)
at Layer.handle [as handle_request] (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:317:13)
at /home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:335:12)
at next (/home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express/lib/router/index.js:275:10)
at /home/projects/44836938-req-checkbody-is-not-a-function/node_modules/express-validator/lib/express_validator.js:445:5
答案 1 :(得分:0)
我得到了它,但我不知道为什么。如果有人能帮我理解,我会很感激。当我删除验证器代码(仅在项目中继续)时,我找到了答案,然后注意到了
req.body
是undefined
用Google搜索了一个答案,它涉及在设置中进一步向下移动路线,所以我尝试了。
我搬了
app.use('/', index);
app.use('/users', users);
app.use('/catalog', catalog); // Add catalog routes to middleware chain.
下面
/ view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator()); // Add this after the bodyParser middlewares!
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
一切正常。
答案 2 :(得分:0)
我遇到了同样的问题,在我的情况下我通过确保app.js中的app.use(expressValidator())在app.use.bodyParser后面解决了它,我确保路由在bodyparser和app.use(express.static ......) 我认为这与元素的放置顺序有关。
就我而言,似乎有效的顺序:
的ViewEngine
BodyParser中间件
路由
静态文件夹
app.use express validator
在使用此命令之前,Checkbody不作为函数存在,但可以使用函数Body。所以请求。似乎工作但不适用于所有功能。
如果有人可以更好地解释,请执行:)