почему axios возвращает ошибку обещания в реагировать - PullRequest
0 голосов
/ 01 ноября 2018

я получаю сообщение об ошибке каждый раз, когда нажимаю кнопку входа

TypeError: Cannot read property 'then' of undefined

, но после перезагрузки ошибка исчезла. я могу знать, что случилось? вот мой код при входе в систему

onSignIn(e){
    e.preventDefault()

    this.Auth.login(this.state.signInUsername, this.state.signInPassword)
      .then(res => {
        this.props.history.replace('/')
      })
      .catch(err => {
        alert(err)
      })
  }

и это мой код авторизации:

login(username, password){
    axios.post('http://localhost:3000/user/login', {
      username,
      password
    })
    .then(this._checkStatus)
    .then(res => {
      if(res.data.success === true){
        const payload = {
          name: username,
        }
        this.setToken(payload)
        return Promise.resolve(res)
      }else{
        console.log(res.data.message)
      }
    })
    .catch(err => {
      return Promise.reject(err)
    })
  }

Ответы [ 2 ]

0 голосов
/ 01 ноября 2018

Я полагаю, у вас есть -tv- ".then" для вашего axios.post. В примере, представленном на сайте axios, используется

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

только один

0 голосов
/ 01 ноября 2018

возврат осей из функции входа в систему.

login(username, password){
    return axios.post('http://localhost:3000/user/login', {
      username,
      password
    })
    .then(this._checkStatus)
    .then(res => {
      if(res.data.success === true){
        const payload = {
          name: username,
        }
        this.setToken(payload)
        return res;
      }else{
        console.log(res.data.message)
      }
    })
    .catch(err => {
      throw err;
    })
  }
...