Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 快速验证程序-自定义密码验证程序无法读取未定义的属性_Javascript_Node.js_Express_Mongoose_Express Validator - Fatal编程技术网

Javascript 快速验证程序-自定义密码验证程序无法读取未定义的属性

Javascript 快速验证程序-自定义密码验证程序无法读取未定义的属性,javascript,node.js,express,mongoose,express-validator,Javascript,Node.js,Express,Mongoose,Express Validator,我有一个简单的验证器,可以检查“password”=“passwordconf” exports.RegisterUser=[ check('username').isLength({ min: 1 , max: 10}).trim().withMessage("Length 1-10"), check('password').isLength({ min: 6 , max: 10}).trim().withMessage("Length 6-10"), check('passwordconf'

我有一个简单的验证器,可以检查“password”=“passwordconf”

exports.RegisterUser=[

check('username').isLength({ min: 1 , max: 10}).trim().withMessage("Length 1-10"),
check('password').isLength({ min: 6 , max: 10}).trim().withMessage("Length 6-10"),
check('passwordconf').isLength({ min: 6 , max: 10}).trim().withMessage("Length 6-10"),
check('passwordconf').custom((value , { req }) => {
    if (value !== req.body.password) {
        throw new Error('Password confirmation is incorrect');
    } 
}),
sanitizeBody('*').trim().escape(),

function ( req , res ) {
    //check for errors
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
      } else {

        var user = new User({
            username : req.body.username,
            password : req.body.password
        });

        var username = req.body.username;
        //check if user is in DB
        User.findOne({ 'username' : username})
            .exec(( err , docs) => {
                if (err) {
                    res.send('There was an error');
                    return err;
                } else if (!docs) {
                    //username does not exist
                    user.save((err) => {
                        if (err) {
                            return next(err)
                        } else {
                            //saved it
                            res.send('saved a new user!')
                        }
                    })
                } else {
                    res.send('Username exists!')
                }                      
        })}
}]

如果我把这句话删去

check('passwordconf').custom((value , { req }) => {
        if (value !== req.body.password) {
            throw new Error('Password confirmation is incorrect');
        } 
    })
代码起作用了。我从官方文件中复制了这句话 我在保存新用户时出现此错误

{"errors":[{"location":"body","param":"passwordconf","value":"password1","msg":"Cannot read property 'then' of undefined"}]}
这里缺少什么?

这是,将在下一个版本中修复(当前版本为v5.2.0)。
不返回任何内容的自定义验证器会因该错误而失败

要解决此问题,您只需从验证器返回
true

check('passwordconf').custom((value , { req }) => {
    if (value !== req.body.password) {
        throw new Error('Password confirmation is incorrect');
    }

    return true;
})

我很确定这是因为没有第三个参数来实际处理请求。(如他们的示例中的
//处理请求
)。哪一行触发了错误?我该如何放置第三个参数?因为我已经有了
检查('passwordconf')。自定义(…),函数(req,res){}
。此外,验证器也会工作,并告诉我pword不匹配是否自v5.3.0版本以来已修复