gpt4 book ai didi

javascript - TypeError : req. checkBody(...).optional(...).isDate 不是函数

转载 作者:行者123 更新时间:2023-11-30 09:29:45 25 4
gpt4 key购买 nike

我在使用快速验证器时遇到问题,特别是 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.

在我的一个 Controller 中,我使用 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},
} );

现在要处理发布请求,我在 Controller 中有以下代码:

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 中删除。可以看到this在 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 的日期。它取自 this回答。 Stack Overflow 上还有许多针对不同格式的其他答案:

或者使用瞬间的isValid() .

关于javascript - TypeError : req. checkBody(...).optional(...).isDate 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com