在我的JSON文件中,我想要調用一些數據層。我的JSON數據,如我的getInitState
所示,組織爲{forthem:[{articles:[]}]}
。您也可以在https://emjayweb.github.io/portfolio/scripts/snippets.json查看我的數據。ReactJS調用JSON API
第一個函數,我使用變量fakeit
按預期工作。然而,第二個函數,我打電話makeit
來訪問我的articles
數據不起作用。我收到一個Cannot read map of undefined
錯誤。
我已經在下面列出了我的完整代碼以供審查。
getInitialState:function(){
return {forname: '', forsym: '', fortitle: '', fulldata: {forthem:[ {articles: [] } ]} }
},
componentDidMount:function(){
var self = this;
$.getJSON('https://emjayweb.github.io/portfolio/scripts/snippets.json', function(snowden){
self.setState({fulldata: snowden});
});
},
render:function(){
return (<div id="madeforlist">
<ul>
{this.state.fulldata.forthem.map(function(fakeit, i){
return <li key={i}>
<i className="fa fa-plus-circle" aria-hidden="true"></i>
<span id="forname">{fakeit.therefore}</span>
<span className="pull-right">{fakeit.theresym}</span>
</li>})}
<ul>
{this.state.fulldata.forthem.articles.map(function(makeit, o){
return <li key={o}>{makeit.article}</li>
})}
</ul>
</ul>
</div>);
}
所以這個工程。但是,我似乎只能調用[0]。如果我用[1]替換它,我會得到一個'undefined'錯誤的文章。 – abrahamlinkedin