TypeError:无法读取未定义的属性“id”(REACTJS)

时间:2018-03-15 15:55:14

标签: javascript reactjs

对于Reactjs来说还是新手,我认为我已尽力而为,但仍无法解决此错误for (i = 0; i < col; i++){ sumC = 0; for (j = 0; j < row; j++) { sumC += matrix[j][i]; if(sumC>1.5){ sumC=0; } } } 请帮助下面是我的代码。

TypeError: Cannot read property 'id' of undefined

错误完整堆栈跟踪:

enter image description here

这是我试图循环的烧瓶中的数据。试图将每个对象转换为数组,然后遍历数组。我也试过console.log(lol),我得到的数据如下图所示     data is available

if(this.props.controller === 'location'){  

   eventData = Object.keys(this.props.heda).map( evKey => {

        return Object.keys(evKey).map( post => {

            return [...Array(this.props.heda[evKey][post])].map( lol => {

                    return  <Event key= {lol['id']}          
                    descriptions = {lol['description']}
                    headings = {lol['title']}
                    id = {lol['id']} 
                    clicked = { ()=> this.viewSelectedEvent(lol['id']) }/>;

            }) ; 

        });

    });

}

3 个答案:

答案 0 :(得分:1)

使用[...Array(this.props.heda[evKey][post])]创建一个数组,其中第一个元素是数组this.props.heda[evKey][post])

也许您想说[...this.props.heda[evKey][post]]来创建数组的克隆?

答案 1 :(得分:0)

因为他无法在this.props.heda[evKey][post]对象中找到id。所以首先你应该控制你的this.props.heda[evKey][post]对象,之后我认为你会得到你的实际问题。

答案 2 :(得分:0)

请尝试以下代码段:

this.props.heda.map((evVal, evKey) => {
    return evVal.map((postVal, postKey) => {
        ...
    }
}

现在this.props.heda [evKey] [postKey]会给你预期的对象。