javascript - 数据直到减速器才到达
问题描述
我在我的反应应用程序 redux saga 中使用。我有一个登录表单。使用 redux saga,我尝试在用户登录时处理错误。贝娄是我的传奇:
function* postLoginUserReq(user) {
const {name} = user.values.user;
try {
const data = yield call(() => {
return fetch("url", {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: name,
}),
}).then(data => data.json()).then(response => {
userErrorLogIn(response.error) //here i check if appears an error
})
});
} catch (error) {
console.log(error);
}
}
贝娄是动作创造者:
export const userErrorLogIn = (error) => {
console.log(error) //the error message appears here
return {
type: USER_ERROR_LOGIN,
payload: error
};
};
波纹管是减速器:
case USER_ERROR_LOGIN: {
console.log(action.payload) //here the error message does not appears (why?)
return {
...state,
userIsLoggedError:action.payload,
}
}
问题:我没有得到减速器错误的问题可能是什么?
解决方案
您可以为此使用 .catch -
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
fetch("http://httpstat.us/500")
.then(handleErrors)
.then(response => console.log("ok") )
.catch(error => userErrorLogIn(error) );
推荐阅读
- angular - 列表顺序更改角度 7
- c# - c# 返回客户名称字符串
- php - 如何显示选中的复选框和未选中的复选框
- alloy - Alloy5 中的默认运行命令:Alloy4 中的等价物是什么?
- python - 如何制作闪烁的文本选项
- regex - 无法将此正则表达式存储在变量中
- sql-server - sql server 分区性能
- jupyter-notebook - 错误 conda.core.link:_execute(700):安装包“conda-forge::jupyter_nbextensions_configurator-0.4.1-py37_0”时出错
- angular - 如何以角度限制最终用户对 URL 的更改
- python - 如何在我的自动点击器中添加左右鼠标按钮之间的切换?