未捕获(承诺)TypeError:无法设置属性'status'

时间:2017-11-26 20:21:59

标签: javascript json ajax vue.js axios

嘿,我有一些问题试图确认我使用Axios with VueJS从我的服务器收到的回复

axios.post('/login', {
  email: this.email,
  password: this.password
}).then(response => {
  if (response.status == 200) {
    $root.$data.auth = true;
  } else {
    alert('uhhhh');
  }
}).catch(e => {
  if (e.response.status == 422) {
    this.error = "You made that login right?  How could you habve forgotten it!";
  } else {
    this.error = "Something else happened and this is what: " + e.response.status;
  }
});

登录成功后,我在控制台中看到了这一点:

Uncaught (in promise) TypeError: Cannot set property 'status' of undefined

这似乎令人困惑,因为Axios文档指出我可以从中拉出response.status对象并运行条件逻辑?

2 个答案:

答案 0 :(得分:0)

如果不是200(OK),你不应该检查响应状态,代码将执行下一个.catch()的回调,在这里你可以处理错误。

答案 1 :(得分:0)

我能够解决以下错误:response.status 未定义错误:

const errStatus = (typeof error.response.status !== 'undefined') ? error.response.status : 'unknown';
const errText = (error.response.statusText != null) ? error.response.statusText : 'unknown';
const errElse = { email: [`[Error status code: ${errStatus}].....[Error status: ${errText}]......Please submit a trouble ticket`] };
this.errors = errElse;