一次显示NodeJS的所有错误

时间:2017-07-21 10:39:20

标签: node.js

我一次遇到问题显示错误,当comparePassword不正确且req.check插入错误时。拜托,有人可以帮我改进吗?

router.put('/changePassword', function (req, res, next) {

User.comparePassword(req.body.old, req.user.password, function (err, isMatch) {
req.check('old', 'Old password is required').notEmpty();
req.check('new', 'New password is incorrect').isLength({ min: 4 }).equals(req.body.newConfirm);

  var errors = req.validationErrors();


      if (err) throw err;
      if (isMatch) {
          bcrypt.hash(req.body.new, saltRounds, function (err, hash) {
          if (err) {
            console.log(err)
          } else {
              var newPassword = hash;
                  User.findOneAndUpdate({ _id: req.user._id },
                    {
                      $set: { 
                        password: newPassword,
                      }
                    },
                    {
                      upsert: true
                    },
                    ((newPassword) => {
                      res.send(newPassword)
                    })
                  )
                } 
    })

  } else {
        res.json('Old password is wrong')
  }}
  )})

1 个答案:

答案 0 :(得分:0)

您必须检查if (err)错误是否为空或未定义(或反过来),然后您可以创建一个包含错误的变量(带{err, errors}的新对象?并扔掉它