我认为这很简单,但我感觉将自定义验证器与现有验证器链接会导致 req 对象发生一些奇怪的事情,这似乎是未定义的:
req.checkBody('Game18awayScore', 'must be between 0 and 30').isInt({min:0, max:30}).custom((value,{ req }) => {
console.log(something);
if (Math.abs(value - req.body.Game18homeScore) < 2){
if (value < 30 && req.body.Game18homeScore < 30){
throw new Error("winning score isn't 2 greater than losing score");
}
}
});
req.checkBody('homeMan1', 'Please choose a player.').notEmpty().custom((value,{req}) => {
if (value != 0){
if (value == req.body.homeMan2 || value == req.body.homeMan3 || value == req.body.awayMan1 || value == req.body.awayMan2 || value == req.body.awayMan3){
throw new Error("can't use the same player more than once")
}
}
});
但我只是不断得到:
TypeError: Cannot destructure property
reqof 'undefined' or 'null'.
第一个习惯是检查两个值之间是否存在至少两个差异,除非其中一个值是 30。
第二个习惯是检查一个值没有在其他 5 个选项中使用。
我应该补充一点,这段代码在验证器函数中:
function validateScorecard (req,res,next){ [all my validations for the form including the ones above] }
然后将其包含在路线中:
app.post('/scorecard-beta',validateScorecard, fixture_controller.full_fixture_post);
有任何想法吗?