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

177
Views
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 answers
Answer question

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 Report

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 Report

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 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!