UnhandledPromiseRejectionWarning: TypeError: res.status (...). json (...). Catch не является функцией - PullRequest
0 голосов
/ 01 августа 2020

Я получаю сообщение об ошибке Type Error: res.status(...).json(...).catch is not a function, когда пытаюсь отправить запрос с помощью почтальона, не знаю, что делаю неправильно.

вход. 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 Ответ

0 голосов
/ 01 августа 2020

Вы потеряли .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)
          })
      })
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...