【发布时间】:2017-03-13 10:42:39
【问题描述】:
我正在使用 fetch 进行异步调用,然后尝试通过根据返回的 json 数据的结果调度操作来设置状态。
我正在使用二维码阅读器读取传递给我的 didScan 方法的代码。
didScan(code) {
if (this.state.showCamera){
this.props.pushNewRoute('finder');
getAppointment(code)
.then((appointment)=>{
if (appointment.valid){
this.props.appointmentDetails(appointment);
this.props.resetRoute('summary');
}else{
this.props.resetRoute('error');
}
})
.catch((error) => {
this.props.resetRoute('error');
});
this.setState({showCamera: false});
}
}
我正在使用 react-redux 将我的操作绑定到我的调度程序,如下所示:
function bindActions(dispatch){
return {
resetRoute:(route)=>dispatch(resetRoute(route)),
pushNewRoute:(route)=>dispatch(pushNewRoute(route)),
appointmentDetails:(details)=>dispatch(appointmentDetails(details))
}
}
export default connect(null, bindActions)(Scanner);
但是当我的 getAppointment 服务返回承诺时,它会在尝试进行路由时失败。
this.props.resetRoute('summary');
错误是
可能未处理的承诺拒绝{id:0} Reducers 可能不会派发动作
我的 reducer 都没有调度任何操作,当我将它从 Promise .then() 块中取出时,代码工作正常。
为了完整起见,这里是简单的 getAppointment 获取服务:
export function getAppointment(id:string) {
return fetch('http://myurl/' + id + '/')
.then((response) => response.json())
.catch((error) => {
console.error(error);
return error;
});
}
非常感谢任何帮助。
【问题讨论】:
标签: react-native ecmascript-6 redux react-redux es6-promise