【问题标题】:Uncaught (in promise) TypeError: Cannot set property 'status'未捕获(承诺中)类型错误:无法设置属性“状态”
【发布时间】:2018-05-10 02:01:58
【问题描述】:

你好,我在尝试使用 Axios 和 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 对象并从中运行条件逻辑,这似乎令人困惑?

【问题讨论】:

  • 报错说无法设置,这里你只是读取状态属性,你在哪里更新状态?
  • 这也让这很奇怪,因为该组件中唯一提到的状态是试图读取该响应对象。 ://
  • 你在代码的其他地方有没有声明.status = something
  • 你能告诉我们发出异常的代码行吗?
  • if (e.response.status == 422) { 这就是它在控制台中尖叫的地方。

标签: javascript json ajax vue.js axios


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我能够通过以下方式摆脱 error.response.status undefined 错误:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2020-09-17
      • 2021-09-25
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多