0

In my JSON file I have a few layers of data that I would like to call from. My JSON data, as shown in my getInitState is organized as {forthem:[{articles:[]}]}. You can also view my data at https://emjayweb.github.io/portfolio/scripts/snippets.json.

The first function, where I'm using variable fakeit works as expected. However, the second function, where I'm calling makeit to access my articles data does not work. I get a Cannot read map of undefined error.

I have included my full code below for review.

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>);
}

2 Answers 2

1

forthem is an array so literally you need to pass an index to get the array articles

try this.state.fulldata.forthem[0].articles.map()

this.state.fulldata.forthem.map(function(fakeit,i){

   fakeit.articles.map(function(article,j){

   })   
})
Sign up to request clarification or add additional context in comments.

1 Comment

So this works. However, I seem only to be able to call [0]. I get an article of undefined error if I replace this with [1].
1

Cannot read map of undefined error is cause of the this.state.fulldata.forthem.articles.map() the articles is undefined.

And I wonder if GitHub.io support cross-domain? If not, this code cannot be working because getJSON cannot get the data from your gist.

1 Comment

I am able to make API calls from my Github. The only problem is, as you mentioned, the articles is undefined. And I am not sure how to define it in React using the map function. EDIT: Got it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.