我有以下组件,其中我引入了一些JSON,并且我正在尝试使用{ this.state.data.items[0]}
获取一些嵌套信息,但我收到错误:Uncaught TypeError: Cannot read property '0' of undefined
,尽管{{1} }和{this.state.data}
同样适用
这是完整的代码:
{this.state.data.items}
答案 0 :(得分:1)
您的getInitialState
应该是这样的
getInitialState: function () {
return {
data: {
items: []
}
};
},
因为render
之前调用了componentDidMount
。您正在尝试从items
获取属性state.data
,但此属性不存在
this.state.data // => returns empty Array
然后你正在尝试获取属性items
this.state.data.items // => returns undefined
因为data
没有属性items
。,
然后您尝试从items
Array
获取第一个元素,但之前的语句返回undefined
,这就是您获得Error
的原因,因为您无法从undefined
获取属性{1}}值
答案 1 :(得分:0)
渲染函数无法从初始状态获取data.item。您应该更改:
select team,DIV,GP,W,L,OTL,PTS,GF,GA,DIFF,GPG,GAA,1 as sort_key
from standings_east t
where exists
(select 1
from
(select DIV,max(PTS) as PTS,max(W) as W from standings_east
group by DIV) a
where t.DIV=a.DIV and t.PTS=a.PTS and t.W=a.W
)
UNION ALL
select team,DIV,GP,W,L,OTL,PTS,GF,GA,DIFF,GPG,GAA,2 as sort_key
from standings_east t
where not exists
(select 1
from
(select DIV,max(PTS) as PTS,max(W) as W from standings_east
group by DIV) a
where t.DIV=a.DIV and t.PTS=a.PTS and t.W=a.W
)
order by sort_key,PTS DESC