无法读取未定义反应的属性“长度”

新手上路,请多包涵
TypeError: Cannot read property 'length' of undefined

这就是编译器在我运行我的 React 应用程序时所说的。我需要做什么?

 request = (start,end) => {
   if(this.state.teams.length < 1){
       axios.get(`${ URL }/teams`)
       .then( response => {
           this.setState({
               teams:response.data
           })
       })
   }

    axios.get(`${ URL }/articles?_start=${start}&_end=${end}`)
    .then( response => {
        this.setState({
            items:[...this.state.items,...response.data]
        })
    })
}

原文由 Kuba Kluzniak 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 157
2 个回答

我建议首先检查道具是否未定义或为空,甚至是否已声明。

例如:-

     const card = props && props.cards && props.cards.length > 0 ?
        props.cards.map((card, i) => {
            return (
                <card >
            )
        }) : '';

并退回您的卡。

原文由 Hasan Zahran 发布,翻译遵循 CC BY-SA 4.0 许可协议

我建议在尝试获取长度之前使用检查来查看“teams”是否未定义。

 if (value === undefined) {
    // value is undefined
}

原文由 Bekah Saugen 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题