Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Recibo el error: TypeError: items.map no es una función, cuando intento agregar mi clave API

Cuando uso este componente, aparece el siguiente error. Cuando usé exactamente la misma función solo con otra API, funcionó y obtuve el resultado de la API.

TypeError: items.map no es una función ApiInfo.render C:/Users/32479/mikiesoftMovies2/cinemaReact-master/src/components/ApiInfo.js:35

 32 | 33 | return ( 34 | <div className = "App"> 35 | <h1> Fetch data from an api in react </h1> { | ^ 36 | items.map((item) => ( 37 | <ol key = { item.id } > 38 | Title: { item.original_title }

Cuando lo pruebo con otra API, funcionó y no obtuve ningún error. Soy nuevo en React, así que no sé qué está mal.

Este es mi código (con la API que me da el error) es solo un código que solicita la información de la API y la coloca en un ol :

 import React from "react"; export class ApiInfo extends React.Component { // Constructor constructor(props) { super(props); this.state = { items: [], DataisLoaded: false }; } // ComponentDidMount is used to // execute the code componentDidMount() { fetch( "https://api.themoviedb.org/3/movie/top_rated?api_key=55957fcf3ba81b137f8fc01ac5a31fb5&language=en-US&page=1") .then((res) => res.json()) .then((res) => { this.setState({ items: res, DataisLoaded: true }); }) } render() { const { DataisLoaded, items } = this.state; if (!DataisLoaded) return <div> <h1> Please wait some time.... </h1> </div> ; return ( <div className = "App"> <h1> Fetch data from an api in react </h1> { items.map((item) => ( <ol key = { item.id } > Title: { item.original_title } </ol> )) } </div> ); } } export default ApiInfo;

y agregaré un ejemplo del json de la API que estoy usando: el json de la API sin minimizar

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Los elementos de películas que está buscando son devueltos por la API de la base de datos de películas en res.results , no en res :

 this.setState({ items: res.results, DataisLoaded: true });

Consulte lareferencia de la API para ver las películas mejor calificadas

Como nota al margen, es una buena práctica manejar los casos de error HTTP, así que considere agregar un bloque catch a su llamada de búsqueda.

about 4 years ago · Juan Pablo Isaza Report

0

Los items variables no son una matriz. Por lo tanto map no es una función. Imprime la variable res y verás lo que se esconde detrás y lo que tienes que asignar a los items .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!