Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
I am suppose to fetch data (name & email) from api. My program is compiled sucessfully but I'm getting errors in console. https://randomuser.me/api
import React, {useState, useEffect} from "react";
import './App.css';

function App() {

  const [ data, setData ] = useState([]);

  useEffect(  ()=> {
    loadData();
    //getData();
  }, []);

  const loadData = async () => {
    await fetch("https://randomuser.me/api")
    .then(response => response.json())
    .then(receiveddata => setData(receiveddata));
  }

  console.log(data);

  return (
    <div className="App">
      <p> Fetch/ Async/ Await</p>
      {data.map(user => (
        <div key={user.id}>{user.name}, {user.email}</div>
      ))}
    </div>
  );
}

export default App;

I'm getting this error on the console,

Uncaught TypeError: data.map is not a function

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You are getting the error because data is not an Array. The response that you are getting back is an object that has two properties results and info. Since your state is an array, set the state to receiveddata.results instead.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Firsteval, your console.log(data) will return nothing, because the state will not be updated when your code will run the console.log() ; so don't expect to get debug info with that.

Next, your response is an object, not an array. You can do this way : setData(receiveddata.results) then you can map() your data.

In your render you should also check if map is not empty like this:

  {data && data.map(user => (
    <div key={user.id}>{user.name}, {user.email}</div>
  ))}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to check the structure of data comping from your API. Your array is inside "results" property. And "title" is an object as well. You can find a working example of your code over here CodeSandbox. Check the console you will better understand the structure of your response

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda