ReactJS: this.props.data.map is not a function

16,570

As pointed out by azium my data is an object and not an array like my results were expecting.

To get the "game" array inside the "games" field I set the state as the following:

this.setState({data: data.data.games.game});

To get the other fields I simply created a new state and passed it through a prop i.e. modified_date:

this.state.date = data.data.games.modified_date

The warning: Each child in an array or iterator should have a unique "key" prop. was fixed by adding a "key" property inside the .map method:

render: function() {
    var gameDate = this.props.data.map(function(data) {
        return (
            <h1 key={data.location} >{data.location} </h1>
        );
    });
Share:
16,570
Simon
Author by

Simon

I am a people oriented individual who is still learning and challenging myself everyday. I am interested in mobile technology and am constantly refining my skills. I enjoy working with new technology and always open to learn more.

Updated on June 27, 2022

Comments

  • Simon
    Simon almost 2 years

    I am trying to get the data from a .json file and displaying the results through a view. Below is the scoreboard.json file.

    {
    "subject":"scoreboard",
    "copyright":"Copyright 2015",
    "data":
       "games":{
         "next_day_date":"2015-07-22",
         "modified_date":"2015-08-04T07:34:50Z",
         "month":"07",
         "year":"2015",
         "game":[
            {
               "game_type":"R",
               "double_header_sw":"N",
               "location":"Some City, State",
            }, 
            {
               "game_type":"R",
               "double_header_sw":"N",
               "location":"Another City, Another State"
            }
         ]
       }
    

    I only want the "data" field and not the "subject" or "copyright" fields and am able to do so through the ajax request below.

    	getInitialState: function() {
    		return {data: []};
    	},
    	componentDidMount: function() {
    		$.ajax({
    		  url: this.props.url,
    		  dataType: 'json',
    		  cache: false,
    		  success: function(data) {
    		  	console.log(data.data);
    		    this.setState({data: data.data});
    		  }.bind(this),
    		  error: function(xhr, status, err) {
    		    console.error(this.props.url, status, err.toString());
    		  }.bind(this)
    		});
    	},

    I pass the state down to the receiving file but, when I try to call it I receive a TypeError: this.props.data.map is not a function

    	render: function() {
    		var gameDate = this.props.data.map(function(data) {
    			return (
    				<h1>{data.modified_date} </h1>
    			);
    		});

    I want to be able to get all the information that is in the "data" field of the scoreboard.json eventually (i.e. data.games.game[] array). The .map function Does not seem to work for this either.

    How would I get the "data" fields (more specifically the modified_date) in the .map function?

    Edit: I can log the modified_date by calling data.games.modified_date but, when trying to set the state of data: this.state.data = data.games.game I receive a new warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of "GameDate"