3-1关于数据访问的问题:fetchNetRepository
来源:5-1 Popular(最热)模块的数据层设计
22不小了
2017-05-23
话不多说先把源码贴出来:
export default class DataRepository{
fetchNetRepository(url){
console.log("testurl"+url);
return new Promise((resolve,reject)=>{
fetch(url).then(response=>response.json())
.then(result=>{
resolve(result);
})
.catch(error=>{
reject(error);
})
})
}
}以上是按照老师所写的代码来写:
下面是具体的调用方法:
load(){
let url=this.getUrl(this.text);
this.dataRepository.fetchNetRepository(url).then(result=>{
this.setState({
result:result
})
}).catch(error=>{
console.log("Test:"+error)
this.setState({
result:JSON.stringify('Test:'+error)
})
})
}当我调用具体地址为:https://api.github.com/search/repositories?q=js&sort=stars
时返回了错误信息: Invariant Violation: Objects are not valid as a React child (found: object with keys {total_count, incomplete_results, items}). If you meant to render
我看了下大概意思应该是返回的数据结构问题:
于是我讲调用方法改为了:
load(){
let url=this.getUrl(this.text);
this.dataRepository.fetchNetRepository(url).then(result=>{
this.setState({
result:JSON.stringify(result)
})
}).catch(error=>{
console.log("Test:"+error)
this.setState({
result:JSON.stringify('Test:'+error)
})
})
}然后就恢复正常,这里希望得到老师的解答。我在按照老师的敲写的代码中那个环节出了问题,如果可能还请老师帮忙分析一下调用这个方法的具体事项
写回答
1回答
-
fetchNetRepository这个方法返回的是一个Promise,按照使用Promise的链式调用方式使用就好了,如果按照视频的步骤做,但发现自己的代码出错的话,可以参照课程中提供的各章节源码对照着看自己哪一步出现了问题,这样能帮你快速定位问题,希望能对你有帮助
012017-05-24
相似问题