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

152
Views
Unable to get data from api using Nextjs

I am working on Next js and i am trying to fetching record via api, but right now i am getting following error

"Cannot read property 'map' of undefined"

Here is my code (components/Blog.jsx)

export default function Blog({ people }) {
    return (
      <div>
        {people.map(person => (
          <>
            <h1>{person.name}</h1>
            <h2>Website: {person.website}</h2>
            Email: <code>{person.email}</code>
          </>
        ))}
      </div>
    );
  };
  
  export const getStaticProps = async () => {
    const response = await fetch('https://jsonplaceholder.typicode.com/users');
    const people = await response.json();
  
    return {
      props: {
        people
      }
    };
  };
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A possible fix is to before you return your Blog() function you do something like this

people = people || []

This will make people have a default so that it cannot be undefined when you try to map it

about 4 years ago · Juan Pablo Isaza Report

0

After reading your problem, I understood that It happens either because of the undefined value of people

export default function Blog({ people }) {
    return (
      <div>
        {people.map(person => (
          <>
            <h1>{person.name}</h1>
            <h2>Website: {person.website}</h2>
            Email: <code>{person.email}</code>
          </>
        ))}
      </div>
    );
  };
  
  export const getStaticProps = async () => {
try{
    const response = await fetch('https://jsonplaceholder.typicode.com/users');
    const people = await response.json();
    return { props: { people} }
  }catch{
    return { props: {  } }
  }
  
  };
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!