【发布时间】:2018-09-30 22:21:52
【问题描述】:
我正在使用这个 API 来制作地图,并且我实施了一些验证,以防在获取数据时出错。实施此解决方案时,出现错误:
未处理的拒绝(TypeError):无法读取未定义的属性“json”
componentDidMount() {
fetch('https://api.citybik.es/v2/networks')
.then(res => { // *** implemented this
if (!res.ok) { // ***
throw new Error(res.status); // ***
} // ***
})
.catch(error => {
console.log(error);
})
.then(res => res.json()) // *** error in this line
.then(response => {
const networkData = response.networks;
this.setState({
bikeData: networkData
});
})
}
谢谢!
【问题讨论】:
-
你不会从一开始就返回任何东西,它只处理错误情况。
-
你有什么建议? @jonrsharpe
-
要么合并它们,要么返回一些东西让第二个处理。
标签: javascript json reactjs api