Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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
Node.js 使用express validator作为中间件验证密码_Node.js_Middleware - Fatal编程技术网

Node.js 使用express validator作为中间件验证密码

Node.js 使用express validator作为中间件验证密码,node.js,middleware,Node.js,Middleware,当密码不匹配时,以下代码不会退出,因为body没有res对象,所以我决定将其设为middleware并使用它 router.post('/signup', [ body('confirmpassword').custom((value, {req, res }) =>{ if(value !== req.body.password) { console.log(value) console.log("passworss didno

当密码不匹配时,以下代码不会退出,因为body没有res对象,所以我决定将其设为middleware并使用它

 router.post('/signup', [
   body('confirmpassword').custom((value, {req, res }) =>{
      if(value !== req.body.password) {
          console.log(value)
          console.log("passworss didnot match")
          return res.end("password and confirm password didinot match") // going to auth.signup controllel
         // throw  new Error ("passwords didnot match")

      }
  })
]
 ,auth.signUp);
在上面的代码中,我导入了我的中间件代码以进行检查

let auth   = require('../controllerls/auth');
     router.post('/signup', validatePassword ,auth.signUp);
这些是我的一段代码,我将其作为中间件制作成seprate文件,现在我不知道发生了什么,我的请求被卡住了,请帮助我

我想使用express validator,因为我想学习它,我知道我们得到了req.body.confirmpassword和req.body.password,并检查它

    const { check, validationResult,body } = require('express-validator');


    const vaiditePassword = (req,res,next) => {
      console.log("we are inside the validate password method")
console.log(req.body.email) // check to see whether i am having access to my req.body seems fine
    return  body('confirmpassword').custom((value, {req }) =>{
      console.log(value)
        if(value !== req.body.password) {
            console.log("passworss didnot match")
            res.send("verfiy password and password didn't match");
        }
        next()
    })

}
module.exports = vaiditePassword
将返回一个函数(您可以称之为中间件),因此内部不会执行任何内容,除非您通过添加两个括号明确要求执行

return  body('confirmpassword').custom((value, {req }) =>{
    console.log(value)
    if(value !== req.body.password) {
        console.log("passworss didnot match")
        res.send("verfiy password and password didn't match");
    }
    next()
})
尽管如此,这样使用它是一种反模式。请更仔细地检查文档中的使用模式,看起来他们希望您从回调中返回值,而不是直接使用
res
对象

另外,既然您自己正在创建一个中间件,为什么要使用
expressvalidator
,这可以简单到

return  body('confirmpassword').custom((value, {req }) =>{
    console.log(value)
    if(value !== req.body.password) {
        console.log("passworss didnot match")
        res.send("verfiy password and password didn't match");
    }
    next()
})(req.body.confirmpassword, { req })

当你有匹配的密码或其他密码时,它会被卡住吗?感谢你的关注,我可以看到代码执行到body.email body('confirm password')。自定义没有被执行。哦,我知道现在发生了什么。对不起,我没有得到。你能详细地添加这个
(req.body.confirmpassword,{req})
紧跟在
custom
函数callreturn body('confirmpassword')。custom((value,{req})=>{(req.body.confirmpassword,{req})console.log(value)还是同一个问题如果您同意,我将向您提供github链接否不是这样,我已经为您的可视化方便添加了更多详细信息谢谢但很抱歉打扰您更多我收到这些错误类型错误:无法在字符串“111”上创建属性“Symbol(express validator#Context)”111是我的确认密码
const vaiditePassword = (req,res,next) => {
    console.log("we are inside the validate password method")
    console.log(req.body.email) // check to see whether i am having access to my req.body seems fine
    if(req.body.confirmpassword !== req.body.password) {
            console.log("passworss didnot match")
            return res.send("verfiy password and password didn't match");
    }
    next()
}