【发布时间】:2019-10-03 09:48:41
【问题描述】:
我的网络很慢,所以我的代码无法正常工作。
当我尝试从本地服务器获取数据时,如果我有良好的连接,我会想到,但如果它是弱连接,我会在网页上遇到一些错误。
TypeError: Cannot read property 'map' of undefined
我想用地图功能写一些我在数据库中的音乐列表,但我收到了这个错误。
所以我想为用户提供更漂亮的这种错误,而不是像这样的错误,也许是一些包或其他东西可以帮助我给我一些建议。
音乐列表显示功能
displayMussics() {
var data = this.props.data;
if(data.loading){
return (
<Dimmer active inverted>
<Loader inverted content='Loading' />
</Dimmer>
)
} else {
return data.mussics.map(mussic => {
return (
<li key={ mussic.id }>{ mussic.name }</li>
);
})
}
}
【问题讨论】:
-
你能粘贴一些代码吗?
-
如果你想检查用户是在线还是离线,你可以这样做stackoverflow.com/a/29802862/2445295。但是如果你想检查网络调用是否因为网络错误而失败,你可以为 fetch API 编写一个拦截器。
-
我添加了我的代码
-
你应该在问题中附加你的代码,而不是作为答案,+,确保你的
this.props.data确实有你想要的数组,否则它会出错
标签: reactjs graphql apollo-server