Javascript TypeError:req.checkBody(…)。可选(…)。isDate不是函数

Javascript TypeError:req.checkBody(…)。可选(…)。isDate不是函数,javascript,node.js,express,express-validator,Javascript,Node.js,Express,Express Validator,我在使用ExpressValidator时遇到了一些问题,特别是isDate函数。我已采取步骤使用expressvalidator、bodyparse、validator模块等。所有路由仅在此之后。。 环境是Node+Express 问题是如何使用 "req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate();" 我不断得到以下错误 TypeError: req.checkBod

我在使用ExpressValidator时遇到了一些问题,特别是isDate函数。我已采取步骤使用expressvalidator、bodyparse、validator模块等。所有路由仅在此之后。。 环境是Node+Express

问题是如何使用

"req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate();" 
我不断得到以下错误

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/svitaworld/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
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.
在我的一个控制器中,我使用isDate()方法对我在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},   
} );
现在,为了处理post请求,控制器中有以下代码:

authorController.js--第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();
isDate()
已从validator.js中删除。有关更多信息,请参见GitHub上的提交。express validator使用validator.js进行验证

您可以创建一个自定义验证器来检查有效日期。对于新API:

check('date').custom(isValidDate).withMessage('the date must be valid');
对于传统API:

app.use(expressValidator({
  customValidators: {
    isValidDate: isValidDate
  }
}));
当您应用中间件(在app.js或类似文件中)并进行检查时:

req.checkBody('date', 'the date must be valid').isValidDate();
isValidDate()
必须由您自己编写。以下是一个例子:

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;
}
这将检查yyyy-mm-dd格式的日期。这是从答案中摘取的。对于堆栈溢出的不同格式,这里还有很多其他答案:


或者使用片刻的。

谢谢。我想我会用矩的isValid()来代替。var dateFormat=“DD/MM/YYYY”;时刻(req.body.date,dateFormat,true)。isValid();在撰写本文时,
isDate()
仍然作为一种方法存在于validator.js文档中。近3年来没有人更新文档了吗?isDate回来了:)。。。在2020/04/06检查此处:您还可以使用isISO8601()检查YYYY-MM-DD日期格式