我目前正在运行2台服务器:
以下是我记录用户的操作:
// Redux Action
export function loginUser(creds, role) {
return dispatch => {
// We dispatch requestLogin to kickoff the call to the API
dispatch(requestLogin(creds));
return axios.post(`${ROOT_URL}/login/${role}`, creds).then((response) => {
console.log(response);
if(response.status === 200) {
// If login was successful, set the token in local storage
localStorage.setItem('id_token', response.data);
// Dispatch the success action
dispatch(receiveLogin(response));
return response;
}
}).catch(err => {
// If there was a problem, we want to
// dispatch the error condition
dispatch(loginError(err.data));
return err;
});
};
}
我故意断开我的数据库以捕获错误,看看会发生什么。所以,这是我在终端中可以看到的:
12:49:24 Project-0 Server is listening at port 3000
12:49:24 Project-0 Mongoose disconnected
12:49:24 Project-0 Mongoose connection error: MongoError: connect ECONNREFUSED 192.168.1.116:27017
12:49:34 Project-0 Wed, 13 Apr 2016 07:19:34 GMT express deprecated res.send(status): Use res.sendStatus(status) instead at app/index.js:61:7
12:49:34 Project-0 OPTIONS /login/admin Wed, 13 Apr 2016 07:19:34 GMT ::ffff:192.168.1.134 200 5.894
12:49:35 Project-0 POST /login/admin Wed, 13 Apr 2016 07:19:35 GMT ::ffff:192.168.1.134 - -
现在,当我提交登录表单时,状态从待处理状态变为已取消。
我们如何使用axios捕获此状态,还是我们必须在表达中为此编写一种机制?
注意:我无法标记axios,因为标签不存在,我无法创建新标签。
答案 0 :(得分:1)
使用 axios.isCancel()
.catch(err => {
if(axios.isCancel(err)) {
// do something
}
}
)
答案 1 :(得分:0)
您实际上在catch语句中捕获错误(不成功的响应)。例如,您可以将其记录到控制台:
.catch(err => {
// If there was a problem, we want to
// dispatch the error condition
dispatch(loginError(err.data));
console.log(err);
return err;
});