admin管理员组文章数量:1326129
I am facing issues using the express validator specifically it is the isDate function. I have taken steps to use expressvalidator, the bodyparse, validator module etc. All routes are after this only.. Environment is Node + Express.
The issue is on the usage of
"req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate();"
and I keep getting the following error.
TypeError: req.checkBody(...).optional(...).isDate is not a function
at exports.author_create_post (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/controllers/authorController.js:47:81)
at Layer.handle [as handle_request] (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at /Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
at next (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3)
at router (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13)
at /Users/svitaworld/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
at next (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
at /Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:635:15
app.js
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.use('/', index);
app.use('/users', users);
app.use('/catalog', catalog); // Add catalog routes to middleware chain.
in one of my controllers, I am using the isDate() method to do some validation on date that I have separately defined int he AuthorSchema.
var AuthorSchema = Schema( {
first_name: {type: String, required: true, max: 100},
family_name: {type: String, required: true, max: 100},
date_of_birth: {type: Date},
date_of_death: {type: Date},
} );
Now to handle the post requests I have this code in the controller:
authorController.js -- line from line 41-48
// Handle Author create on POST
exports.author_create_post = function(req, res, next) {
console.log("DEBUG: starting in exports.author_create_post");
req.checkBody('first_name', 'First name must be specified.').notEmpty();
req.checkBody('family_name', 'Family name must be specified.').notEmpty();
req.checkBody('family_name', 'Family name must be alphanumeric text.').isAlpha();
req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate(); // Error is on this usage of isDate()
req.checkBody('date_of_death', 'Invalid date').optional({ checkFalsy: true }).isDate();
I am facing issues using the express validator specifically it is the isDate function. I have taken steps to use expressvalidator, the bodyparse, validator module etc. All routes are after this only.. Environment is Node + Express.
The issue is on the usage of
"req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate();"
and I keep getting the following error.
TypeError: req.checkBody(...).optional(...).isDate is not a function
at exports.author_create_post (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/controllers/authorController.js:47:81)
at Layer.handle [as handle_request] (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at /Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
at next (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3)
at router (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13)
at /Users/svitaworld/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
at next (/Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
at /Users/myputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:635:15
app.js
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.use('/', index);
app.use('/users', users);
app.use('/catalog', catalog); // Add catalog routes to middleware chain.
in one of my controllers, I am using the isDate() method to do some validation on date that I have separately defined int he AuthorSchema.
var AuthorSchema = Schema( {
first_name: {type: String, required: true, max: 100},
family_name: {type: String, required: true, max: 100},
date_of_birth: {type: Date},
date_of_death: {type: Date},
} );
Now to handle the post requests I have this code in the controller:
authorController.js -- line from line 41-48
// Handle Author create on POST
exports.author_create_post = function(req, res, next) {
console.log("DEBUG: starting in exports.author_create_post");
req.checkBody('first_name', 'First name must be specified.').notEmpty();
req.checkBody('family_name', 'Family name must be specified.').notEmpty();
req.checkBody('family_name', 'Family name must be alphanumeric text.').isAlpha();
req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate(); // Error is on this usage of isDate()
req.checkBody('date_of_death', 'Invalid date').optional({ checkFalsy: true }).isDate();
Share
Improve this question
edited Nov 1, 2017 at 17:39
Mika Sundland
19k16 gold badges40 silver badges51 bronze badges
asked Nov 1, 2017 at 13:41
Sudhakar RSudhakar R
6279 silver badges18 bronze badges
1 Answer
Reset to default 6isDate()
has been removed from validator.js. You can see this mit on GitHub for more information. express-validator uses validator.js for validation.
You can make a custom validator to check for valid dates. For the new API:
check('date').custom(isValidDate).withMessage('the date must be valid');
For the legacy API:
app.use(expressValidator({
customValidators: {
isValidDate: isValidDate
}
}));
when you apply the middleware (in app.js or something similar) and for checking:
req.checkBody('date', 'the date must be valid').isValidDate();
isValidDate()
has to be written by yourself. Here is an example:
function isValidDate(value) {
if (!value.match(/^\d{4}-\d{2}-\d{2}$/)) return false;
const date = new Date(value);
if (!date.getTime()) return false;
return date.toISOString().slice(0, 10) === value;
}
This checks for dates with the yyyy-mm-dd format. It was taken from this answer. There are also plenty of other answers for different formats here on Stack Overflow:
- mm/dd/yyyy
- dd/mm/yyyy
Or use moment's isValid().
本文标签: javascriptTypeError reqcheckBody()optional()isDate is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: req.checkBody(...).optional(...).isDate is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191235a2430253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论