question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

has no method checkBody

See original GitHub issue

I am using express 4.11.1 and express-validator 2.8.0.

here is the code i used above post action:

var util = require('util');
var express = require('express');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var app = express();

and inside post action i gave

req.checkBody(‘postparam’, ‘Invalid postparam’).notEmpty().isInt();

i am getting has no method checkBody error, don know whether version is the problem so mentioned the versions above.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
jeroenpelgrimscommented, Dec 1, 2015

Just a clarifying comment on this issue: make sure you “use” your routers after you have added expressValidator. (Which makes sense but I had too look for this for a while)

var bodyParser = require('body-parser');
var expressValidator = require('express-validator');

app.use(bodyParser.urlencoded());
app.use(expressValidator());

// Now we can start adding routers to our app
app.use('/example', exampleRouter);
2reactions
eyedealistcommented, Feb 5, 2015

Looks like you forgot to register the bodyParser and expressValidator middleware with the app (which will add the validation methods to the request object).

Try:

var util = require('util');
var express = require('express');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// add validation methods to request
app.use(expressValidator());

// now the methods are available
app.post('/', function(req, res, next) {
    req.checkBody('postparam', 'Invalid postparam').notEmpty().isInt();
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

"req.checkBody is not a function"? - Stack Overflow
When I submit a form whose contents are validated using express-validator I keep getting the error "req.checkBody is not a function" (and if ......
Read more >
How to use notEmpty function in Validator - Javascript - Tabnine
req.checkBody('password', 'Password is empty!!').notEmpty()
Read more >
koa-validate - npm
checkBody (fieldName,[transFn]) - check POST body. ,transFn see json-path.it will not use json path if transFn is false . checkQuery(fieldName,[ ...
Read more >
Legacy API - express-validator
It's based around setting a global middleware in your express app and decorating the request object with new methods. This API MUST NOT...
Read more >
req.checkBody(…).optional(…).isDate is not a function” - Learn
Need help finding rootcause of “TypeError: req.checkBody(…).optional(…).isDate is not a function” I am trying to find the root cause of this error I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found