代码之家  ›  专栏  ›  技术社区  ›  Morton

使用节列表时无法读取未定义的属性“length”

  •  1
  • Morton  · 技术社区  · 7 年前

    我调用API来获取 movieData 应用程序编程接口: https://obscure-reaches-65656.herokuapp.com/api/getCloseTime?city=Hsinchu&sTime=18&eTime=21

    我认为数据是正确的 SectionList ,数据是一个包含大量对象的数组,但我仍然得到错误:

    Cannot read property 'length' of undefined

    无法解决数据问题?

    这是 console.log(movieData); :

    enter image description here

    以下是关于我的类组件呈现:

    render() {
        const movieData = this.props.searchTime;
    
        if (this.props.loading) {
          return <Spinner text='Loading...' />;
        }
        console.log('movieData =>');
        console.log(movieData);
    
        return (
          <View>
            <SectionList
              renderSectionHeader={this.sectionComp}
              renderItem={this.renderSectionItem}
              sections={movieData}
              ItemSeparatorComponent={() => <View><Text></Text></View>}
              ListHeaderComponent={() => <View style={{ backgroundColor: '#25B960', alignItems: 'center', height: 30 }}><Text style={{ fontSize: 18, color: '#ffffff' }}>Header</Text></View>}
              ListFooterComponent={() => <View style={{ backgroundColor: '#25B960', alignItems: 'center', height: 30 }}><Text style={{ fontSize: 18, color: '#ffffff' }}>Footer</Text></View>}
            />
          </View>
        );
      }
    }
    

    如果我设置测试数据 testSections 而不是 电影数据 它起作用了。

    const testSections = [
      { key: "A", data: [{ title: "test1-1" }, { title: "test1-2" }, { title: "test1-3" }] },
      { key: "B", data: [{ title: "test2-1" }, { title: "test2-2" }, { title: "test2-3" }, { title: "test2-4" }, { title: "test2-5" }] },
      { key: "C", data: [{ title: "test3-2" }, { title: "test3-2" }] },
      { key: "W", data: [{ title: "test4-1" }, { title: "test4-2" }, { title: "test4-3" },{ title: "test4-4" }, { title: "test4-5" }, { title: "4-6" }] },
    ];
    

    我的自定义呈现功能如下:

      renderSectionItem = (info) => {
        console.log('what is _renderItem info =>');
        console.log(info);
        const txt = '  ' + info.item.theaterCn;
        return <Text
          style={{ height: 60, textAlignVertical: 'center', backgroundColor: "#ffffff", color: '#5C5C5C', fontSize: 15 }}>{txt}</Text>
      }
    
      sectionComp = (info) => {
        console.log('what is  _sectionComp info =>');
        console.log(info);
        const txt = info.section.key;
        return <Text
          style={{ height: 50, textAlign: 'center', textAlignVertical: 'center', backgroundColor: '#9CEBBC', color: 'white', fontSize: 30 }}>{txt}</Text>
      }
    

    我不知道为什么。

    任何帮助都将不胜感激。事先谢谢。

    2 回复  |  直到 7 年前
        1
  •  7
  •   Prasun    7 年前

    数据结构 movieData 不正确, SectionList 希望每个对象都有一个对象数组 title data 属性,查看文档 here . 所以,根据你的 电影数据 ,您需要将值转换为该结构,例如

    样品

    首先,将获取的数据转换为适当的结构

    fetch('https://obscure-reaches-65656.herokuapp.com/api/getCloseTime?city=Hsinchu&sTime=18&eTime=21')
    .then(r => r.json())
    .then(r => {
      const movieData = r.reduce((r,s) => {
        r.push({title: s.theater, data: s.movie});
        return r;
      }, []);
    
      this.setState({ movieData });
    });
    

    一旦转换了数据,只需将其传递给

    <SectionList
          renderItem={({item, index, section}) => <Text key={index}>{item.enName}</Text>}
          renderSectionHeader={({section: {title}}) => (
            <Text style={{fontWeight: 'bold'}}>{title}</Text>
          )}
          sections={this.state.movieData}
          keyExtractor={(item, index) => item.enName + index}
    />
    

    希望这会有所帮助!

        2
  •  1
  •   Redmen Ishab    6 年前

    我也犯了同样的错误:

    无法从deltapatcher.js中读取未定义长度的属性

    我的解决方案是使用react本机调试器而不是浏览器控制台。

    把它扔了,所以它可能对某人有帮助。

    推荐文章