Search code examples
javascriptarraysreactjsapipokemon-go

How to fix the data.map not a function api


I'm trying to have and show the data of an api .

https://pokeapi.co/api/v2/pokemon/1

I have tried to display the data but it's not working.

PokemonSpecies.js

import React from "react";
// import Loader from "./Loader";
import { Card, Col } from "react-bootstrap";

import { Container, Row } from "react-bootstrap";
import CardPokemon from "./containers/CardPokemon";

class PokemonSpecies extends React.Component {
  state = {
    data: [],
    isLoading: false,
    abilities: []
  };

  async componentDidMount() {
    const id = this.props.match.params.id;

    this.setState({ isLoading: true });
    try {
      const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
      const json = await response.json();
      this.setState({
        data: json,
        isLoading: false
      });
      console.log({ json });
    } catch (err) {
      console.log(err.msg);
      this.setState({ isLoading: false });
      throw err;
    }
  }

  render() {
    const { data } = this.state;
    return (
      <div className="Pokemon">
        <Container>
          <Row>
            <Col lg="4"></Col>
            <Col lg="4">
              <CardPokemon data={data} />
            </Col>
            <Col lg="4"></Col>
          </Row>
          <Row></Row>
        </Container>
      </div>
    );
  }
}

export default PokemonSpecies;

CardPokemon.js

import React from "react";

import DataSinglePokemon from "./DataSinglePokemon";

const CardPokemon = ({ data }) => {
  return (
    <>
      {data.map((info, index) => (
        <DataSinglePokemon
          key={info + index}
          id={info.id}
          name={info.name
            .toLowerCase()
            .split(" ")
            .map(letter => letter.charAt(0).toUpperCase() + letter.substring(1))
            .join(" ")}
          height={info.height}
          {...info}
        />
      ))}
    </>
  );
};

export default CardPokemon;

DataSinglePokemon.js

import React from "react";
import { Card, Col } from "react-bootstrap";
import "../../App.css";
const DataSinglePokemon = props => {
  const { height, name, id } = props;

  const urlImage = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png?raw=true`;
  return (
    <Col lg={3}>
      <Card>
        <Card.Header>{id}</Card.Header>
        <Card.Body className="mx-auto">
          <Card.Title> {name} </Card.Title>
          <Card.Text>
            <img alt={name} src={urlImage} />
            <br></br>
            Taille : {height}
          </Card.Text>
        </Card.Body>
      </Card>
    </Col>
  );
};

export default DataSinglePokemon;

I have the json, but when I'm trying to display the name or the abilities of the Pokemon I have this error I have try a lot of things but I'm new on React js... :

TypeError: data.map is not a function
CardPokemon
src/components/containers/CardPokemon.js:7
   4 | 
   5 | const CardPokemon =({data}) =>{
   6 |     
>  7 |   return(
   8 |   
   9 |   <>
  10 |      {data.map((info,index) =>(

Solution

  • I guess the problem at below line. Can you please check the response of the API call.

    const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
    const json = await response.json();
       this.setState({ 
          data: json,  // Check the data type of json. It should be in Array.
          isLoading: false
       });