0

So im not 100% sure that this is the problem, but Im using the fetch method to grab data from this link https://swapi.py4e.com/api/people . And then im trying to use the .map function on it. Instead of working, I get a Unhandled Rejection (TypeError): jedis.map is not a function error.

When I console logged jedis I got everything you see in that link, like count: 87, next: "https://swapi.py4e.com/api/people/?page=2", etc.

Im assuming that the error means that jedis is actually a object and not an array. But again, im not totally sure thats the reason. If it is, how can I access the data so that jedis is an array and not an object

import React from 'react';
import './App.css';
import CardList from './CardList'

class App extends React.Component{
  constructor() {
    super();
    this.state = {
      jedis: []
    }
  }

  componentDidMount() {
    fetch('https://swapi.py4e.com/api/people').then(resp => resp.json())
    .then(jedi => this.setState({jedis: jedi}));
  }


  render() {
    return (
      <CardList jedis={this.state.jedis}/>

      )
  }

}

export default App;
import React from 'react'
import Card from './Card'

const CardList = ({ jedis }) => {
    return (
        <div>
            {
                //console.log(jedis)
                jedis.map((jedi, i) => {
                    return (
                        <Card
                            key = {i}
                            name = {jedi.name}
                            height = {jedi.height}
                            mass = {jedi.mass}
                        />
                        )

                })

            } 
        </div>

    )   


}


export default CardList;

Heres the API im using incase anyone is wondering http://swapi.py4e.com/

3
  • jedis.results.map? Commented May 14, 2020 at 2:49
  • Already tried that but it didnt work. Just got a TypeError: Cannot read property 'map' of undefined error Commented May 14, 2020 at 2:53
  • this is because you are initialising state as this.state = { jedis: [] }. Change that to this.state = { jedis: { results: [] } } Commented May 14, 2020 at 2:54

3 Answers 3

1

Based on the response of the API,

Change your CardList component props to:

<CardList jedis={this.state.jedis.results} />

Also, then you need to change your state initialisation to:

this.state = {
  jedis: {
    count: 0,
    previous: null,
    next: null,
    results: []
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh i see now what you meant from before. Yes this worked great. Thank you
0

Assuming you want to keep adding to the array on every fetch, you will want to use the spread operator and previousState.

fetch('https://swapi.py4e.com/api/people').then(resp => resp.json())
.then(jedi => { 

    this.setState((previousState) => {
      return {
        jedis: [...previousState.jedis, jedi.results],
      };
    });

});

Comments

0

It's quite simple. Actually you need to convert the response object to Array and you will be good to run map on it. Below is an example assume you get numbers as keys, if your obj response keys are not numbers but regular strings you can replace Number(key) with key in return statement.

var obj = { "1": 5, "2": 7, "3": 0, "4": 0}; var result = Object.keys(obj).map(function (key) { return [Number(key), obj[key]]; });

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.