提问者:小点点

UnhandledPromiserEjectionWarning:TypeError:res.status(…).json(…).catch不是函数


我收到一个错误type error:res.status(。。。)。json(。。。)。catch is not a function当我试图使用postman发布请求时,不知道我做错了什么。

signin.js

exports.signin = (req, res) => {
  const { email, password } = req.body;
  if (!email || !password) {
    res.status(422).json({
      error: "please enter email and password"
    })
  }
  User.findOne({ email: email })
    .then(SavedUser => {
      if (!SavedUser) {
        return res.status(400).json({
          error: "invalid email or password"
        })
      }
      bcrypt.compare(password, SavedUser.password)
        .then(doMatch => {
          if (doMatch) {
            res.json({
              message: "Successfully Signed in"
            })
          }
          else {
            return res.status(422).json({
              error: "Invalid email or password"
            })
              .catch(err => {
                console.log(err);
              })
          }
        })
    })

}

共1个答案

匿名用户

您放错了.catch(。。。),它应该在.then(。。。)之后,而不是res.json():


  exports.signin = (req, res) => {
    const { email, password } = req.body
    if (!email || !password) {
      res.status(422).json({
        error: 'please enter email and password'
      })
    }
    User.findOne({ email: email })
      .then(SavedUser => {
        if (!SavedUser) {
          return res.status(400).json({
            error: 'invalid email or password'
          })
        }
        bcrypt.compare(password, SavedUser.password)
          .then(doMatch => {
            if (doMatch) {
              res.json({
                message: 'Successfully Signed in'
              })
            } else {
              return res.status(422).json({
                error: 'Invalid email or password'
              })
            }
          })
          .catch(err => { // .catch goes here
            console.log(err)
          })
      })
  }