【发布时间】:2021-07-06 22:34:36
【问题描述】:
在axios 库中,404 错误不会直接返回为res.status,而是作为错误在 catch 中抛出。
我现在的问题是,我可以在 catch 中以某种方式查询错误的编号吗?
.catch(error => {
if(error.status === 404)
// do something
}
或者如何使用axios库来查询是否是404错误?
const handleLogin = () => {
axios.post('localhost:4000', {"email":`${email}`, "password":`${password}`})
.then(res => {
console.log(res);
console.log(res.data);
console.log(res.status);
if(res.status === 200) {
setValidationNoExist(true);
history.push({
pathname: '/app',
state: {
email: email,
name: password,
},
})
}
else if(res.status === 203) {
setValidationWrong(false);
}
else if(res.status === 404) {
setValidationPending(false);
}
else if(res.status === 204) {
setValidationNoExist(false);
}
})
.catch(error => {
console.log(error)
setValidationNoExist(false);
})
};
【问题讨论】: