req.checkBody不是一个函数

我正在使用Mozilla Express教程( https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/forms ),并使用express-validator来访问该部分。 当我提交一个表单,其内容使用express-validator进行validation时,我总是收到错误“req.checkBody不是一个函数”(如果我删除了生成该行的行,则会得到其他错误,如“req.sanitize is not一个函数“)。 似乎表示validation程序没有被正确识别。 我使用节点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.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(); 

我哪里错了? 我已经安装并卸载它,我已经从模块中删除了代码,然后重新input,但无济于事。

  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 

也许是你在代码中使用的方式。 因为你没有插入代码的那部分,我假设了一些项目。 无论如何,你可以检查一个工作代码,并找出你做错了什么。 或者,在这里粘贴整个代码,我将能够帮助你。

尝试这个

 // 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 ,你会看到validation工作..错误将是这样的事情

 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 

我得到它的工作,但我不知道为什么。 如果有人能帮助我理解,我会很感激的信息。 当我删除validation器代码(只是为了继续在项目中),然后注意到这一点,我追查了答案

 req.body 

undefined

谷歌search了一个答案,它涉及在设置中进一步下移路线,所以我试了一下。

我搬家了

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

一切正常。

我有同样的问题,在我的情况下,我解决了这个问题,确保在App.js app.use(expressValidator())是在app.use.bodyParser后面,我确保路由是在bodyparser和应用程序.use(express.static ……)我认为这与元素的放置顺序有关。

在我的情况下,似乎工作的顺序:

视图引擎

BodyParser中间件

路线

静态文件夹

app.use表示validation器

在使用这个命令之前Checkbody不是作为一个函数存在的,但是可以使用函数Body。 所以要求。 似乎工作,但不是所有的function。

如果一个人可以更好地解释请做:)