【发布时间】:2018-08-12 03:30:54
【问题描述】:
我有以下代码:
export function fetchValueFromApi(){
return function act(dispatch){
dispatch(fetchingLimit);
return fetch('https://someapi/v1/foo/bar?api_key=123456789')
.then(response =>{
console.log("response",response.json());
response.json();
}).then(json =>{
if (json.data()) {
dispatch(receivingValue(json.data.result))
} else {
throw new Error('Error');
}
}).catch(error => dispatch(receivingValueFailed(error)))
}
}
现在我知道这个调用不会成功。我期待它失败并陷入困境。但是,我收到此错误:
可能的未处理承诺拒绝
所以由于某种原因,我们没有点击.catch。
我该如何解决这个问题?
【问题讨论】:
-
嗯,你的 catch 处理程序也会抛出错误吗?
-
当我将它添加到捕获
console.log('Catching the error', error);时,它会打印:TypeError: Cannot read property 'data' of undefined。但实际上我知道端点会抛出 403 -
哦,我认为无论http状态如何,fetch都会返回成功。
-
顺便说一句,您在
response.json()之前错过了一个回报
标签: javascript react-native redux promise