Need help finding rootcause of “TypeError: req.checkBody(…).optional(…).isDate is not a function”

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 am getting when using isDate() function as prescribed in your tutorial. Everything else works. Any pointers??

In /controllers/authorController.js I have this piece of code to create an author.

// 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(); //We won't force Alphanumeric, because people might have spaces.
  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();
  req.checkBody('date_of_death', 'Invalid date').optional({ checkFalsy: true }).isDate();
.....

Everytime I try to add the author, it gets stuck on the isDate() function.

The error I get is "

> TypeError: req.checkBody(...).optional(...).isDate is not a function
>     at exports.author_create_post (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/controllers/authorController.js:47:81)
>     at Layer.handle [as handle_request] (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
>     at next (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:137:13)
>     at Route.dispatch (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:112:3)
>     at Layer.handle [as handle_request] (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
>     at /Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:281:22
>     at Function.process_params (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
>     at next (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
>     at Function.handle (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3)
>     at router (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12)
>     at Layer.handle [as handle_request] (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
>     at trim_prefix (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13)
>     at /Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7
>     at Function.process_params (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
>     at next (/Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
>     at /Users/MyComputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:635:15

Hi there,

Have you tried check your code against the finished source?

Dear Chris,

Yes, I have verified with finished source. Later I filed a bug

https://bugzilla.mozilla.org/show_bug.cgi?id=1413781

I got the code to work only after I removed the toDate() call since isDate() has been removed from validator.js.

chrismills
November 3

Suds:
Need help finding rootcause of “TypeError: req.checkBody(…).optional(…).isDate is not a function”

Hi there,

Have you tried check your code against the finished source?

github.com

mdn/express-locallibrary-tutorial/blob/master/controllers/authorController.js

> var Author = require('../models/author')
> var async = require('async')
> var Book = require('../models/book') // Display list of all Authors
> exports.author_list = function(req, res, next) { Author.find() .sort([['family_name', 'ascending']]) .exec(function (err, list_authors) { if (err) { return next(err); } //Successful, so render res.render('author_list', { title: 'Author List', author_list: list_authors}); }) }; // Display detail page for a specific Author
> exports.author_detail = function(req, res, next) {

OK, thanks. We are intending to update the module soon, to fix such problems. I’ve included your bug in the project brief of what needs to be done.

Thanks Chris, while you are comipiling that list kindly include this other bug also
https://bugzilla.mozilla.org/show_bug.cgi?id=1413825

Two changes suggested for your docs to make the code work:

req.sanitize(‘genre’).escape(); // error here should be replaced by req.sanitize__(‘genre.*’).__escape();

req.sanitize(‘genre’).escape(); needs to replaced by

and in another place req.body.genre.split(",") needs to be replaced by req.body.genre.toString(). split(",")

Already got that one too :wink: