Uncaught TypeError:无法从道具中读取未定义的属性“ length”

时间:2019-12-13 11:15:46

标签: reactjs jsx

我堆积了这么久。

关于以下代码,当我从状态获取成员对象时,它不被识别为数组对象。

所以我从复制文本中写入了dummy_member,它可以工作并被识别为数组对象。

    render() {
        const members = this.props.members; // <- Get from state.
        console.log(members); //Show Array Object (1)
        console.log(members.length); //Error

        const dummy_members = [{xx:xxx},..]; // <- Copied & paste text from JSON data on browser from server.
        console.log(dummy_members); // Show Array Object (2)
        console.log(dummy_members.length); // It shows array length.

console.log均显示以下结果。

(12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 3, away_team_p1_shots: 20, away_team_p2_score: 4, …}
1: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 1, away_team_p1_shots: 13, away_team_p2_score: 2, …}
2: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 1, away_team_p1_shots: 15, away_team_p2_score: 2, …}
3: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 2, away_team_p1_shots: 15, away_team_p2_score: 3, …}
4: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 6, away_team_p1_shots: 23, away_team_p2_score: 9, …}
5: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 0, away_team_p1_shots: 10, away_team_p2_score: 2, …}
6: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 1, away_team_p1_shots: 12, away_team_p2_score: 1, …}
7: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 1, away_team_p1_shots: 19, away_team_p2_score: 1, …}
8: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 0, away_team_p1_shots: 10, away_team_p2_score: 2, …}
9: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 3, away_team_p1_shots: 16, away_team_p2_score: 4, …}
10: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 0, away_team_p1_shots: 8, away_team_p2_score: 1, …}
11: {away_team_ot_score: 0, away_team_ot_shots: 0, away_team_p1_score: 0, away_team_p1_shots: 8, away_team_p2_score: 1, …}
length: 12

似乎没有错。

我的问题是成员(1)和dummy_members(2)都具有相同的控制台日志,但是结果却不同。道具(1)的成员未被识别为数组。

我想从this.props.members获取成员对象。

谢谢。

1 个答案:

答案 0 :(得分:1)

如果vcgencmd display_power 0 tvservice --off 并不总是包含reducer01数组,则members将是未定义的,而const members = reducer01.members将抛出您观察到的错误。

要防止未定义,可以使用members.length

(为帮助您修复减速器,您必须在问题中共享减速器的代码。)