1

The goal is to pull in the nested array "records". My current output displays the array within the react console, but with an error. I will try and be as concise as possible but will keep things short.

The error screen has 3 lines that are referencing _getRecords so im positive that _getRecords is the problem child.

class RecordBox extends Component {
    constructor() {
    super();

  this.state = {
    showComments: false,
    results: []
  };
 }
 render(){
   const records = this._getRecords();
   return (
      // jsx template code...
   );
  }
  // API Call
 _fetchRecords() {
  $.ajax({
   method: 'GET',
   url: 'http://apidata:8888/data.json',
   success: (results) => {
     this.setState({ results });
   },
   error: () => {
     console.log('error');
   }
  });
 }
 _getRecords() {
    // TypeError: this.state.results.map is not a function...
    return this.state.results.map((record) => {
      return <Comment body={record["Contractor Name"]} />
    });
  }
}

I have a feed that looks like the below. I do not have permission to modify this.

{
"result": {
  "records": [
    {
      "id": 1,
      "email": "[email protected]",
      "author": "Clu",
      "Contractor Name": "Just say no to love!!",
      "avatarUrl": "https://placeimg.com/200/240/any"
    },{
      "id": 2,
      "email": "[email protected]",
      "author": "Anne Droid",
      "Contractor Name": "I wanna know what love is...",
      "avatarUrl": "https://placeimg.com/200/240/any"
    }
  ]
 }
}

REACT Console

12
  • 2
    why is _getRecords outside class? Commented Jun 14, 2017 at 20:05
  • What are you storing in this.state.results and when? Commented Jun 14, 2017 at 20:07
  • @arkjoseph How do you call _getRecords and from where? Commented Jun 14, 2017 at 20:10
  • did you bind.(this) to the _getRecords function? Commented Jun 14, 2017 at 20:10
  • 1
    @arkjoseph check the answer that I have posted.It should solve your problem. Commented Jun 14, 2017 at 20:42

2 Answers 2

1

I think you just aren't setting the state to the right thing. Your state.results is currently an object. Just make sure when you set your state, you set state.results to results.result.records

this.setState({ results: results.result.records })

One thing you could also do is map the results directly in the jsx code and skip using the _getRecords function.

<div>
   {
     this.state.results.map( ( record ) => {
       return <Comment />
     }
   }
</div>

This is the way I usually write this as it's easier for me to read, but it's personal preference.

I hope this helps!

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

Comments

0

The _fetchRecords function needs to change to:-

_fetchRecords() {
  $.ajax({
   method: 'GET',
   url: 'http://apidata:8888/data.json',
   success: (results) => {
     this.setState({ results: results.result.records });
   },
   error: () => {
     console.log('error');
   }
  });
 }

Comments

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.