Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 未捕获类型错误:无法读取属性';长度';从道具中未定义的属性_Reactjs_Jsx - Fatal编程技术网

Reactjs 未捕获类型错误:无法读取属性';长度';从道具中未定义的属性

Reactjs 未捕获类型错误:无法读取属性';长度';从道具中未定义的属性,reactjs,jsx,Reactjs,Jsx,我累得太久了 关于下面的代码,当我从state获取members对象时,它不会被识别为数组对象 所以我从复制文本中编写了dummy_成员,它工作并被识别为数组对象 render() { const members = this.props.members; // <- Get from state. console.log(members); //Show Array Object (1) console.log(members.le

我累得太久了

关于下面的代码,当我从state获取members对象时,它不会被识别为数组对象

所以我从复制文本中编写了dummy_成员,它工作并被识别为数组对象

    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.
这似乎没有错

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

我想从this.props.members获取members对象


谢谢。

如果
reducer01
不总是包含
成员
数组,则
const members=reducer01。成员
将未定义且
成员。长度
将抛出您观察到的错误

为了防止未定义,可以使用
console.log(members&&members.length)


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

它是数组还是类似数组的对象您知道如何识别它们吗?或者如何将它们转换为另一个对象?您可以使用数组的
isArray
进行检查。如果道具数据来自异步操作,初始渲染中没有道具数据。因此它抛出一个错误。const initData={members:[],}导出函数reducer01(state=initData,action){switch(action.type){case'GET_members':返回{…state,members:action.members};默认值:返回状态;}请添加负责获取数据的
MapStateTops()
方法和
Action creator
,我想你是对的。但是我该怎么办呢。我的理解是在
this初始化。state={members:[]}
就足够了。这是我的误解吗?现在我的代码有
导出默认连接((状态)=>state)(第01页)
初始状态仅使用一次,如果
action.members
未定义,则无论初始化如何,还原程序都将返回一个状态为
{members:undefined}
(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