我有这样的代码片段:
(...).catch(e) => {
if(e.response.status === 401) {
console.log("Wrong username or password")
} else {
console.log(e.statustext)
}}
我有这样的错误:
Unhandled Exception (TypeError): cannot read property status of undefined
我该如何解决?
最佳答案
catch
块传递了一个Error对象,它不包含任何名为response
的属性。
在then()
块内,检查状态码是否为401,如果是,则将消息"Wrong username or password"
引发错误,并在catch块内,使用Error.prototype.message记录该消息
.then(response => {
if (response.status === 401) {
throw new Error("Wrong username or password");
}
....
})
.catch(e) => console.log(e.message));
关于reactjs - 如何通过检查reactjs中的错误来修复错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62265698/