0

I am doing a personal project on Next.js with TypeScript. I am trying to fetch an api and then to do a map of the items, but I cannot make it work. I did a console.log and it gives undefined. I did put the file on the pages folder. This is the code:

import { InferGetStaticPropsType } from 'next'

type Data = {
    id: string
    films: string
    people: string
    planets: string
    species: string
    vehicles: string
 }

export const getStaticProps = async () => {
    const res = await fetch('https://www.swapi.tech/api/')
    const swapis: Data[] = await res.json()
  
    return {
        props: {
          swapis,
        },
    } 
}
 
const FilmList = ({ swapis }: InferGetStaticPropsType<typeof getStaticProps>) => {
    console.log('swapis', swapis)
    return (
        <>
        <h2>List of Films</h2>
        {swapis.map((swapi) => (
            <ul key={swapi.id}>
                <li>{swapi.films}</li>
            </ul>
        ))}
        </>
    );
}
 
export default FilmList;
0

1 Answer 1

4

Your Data type is wrong. You're calling https://www.swapi.tech/api/ which returns

{
  "message": "ok",
  "result": {
    "films": "https://www.swapi.tech/api/films",
    "people": "https://www.swapi.tech/api/people",
    "planets": "https://www.swapi.tech/api/planets",
    "species": "https://www.swapi.tech/api/species",
    "starships": "https://www.swapi.tech/api/starships",
    "vehicles": "https://www.swapi.tech/api/vehicles"
  }
}

Full working example:

import { InferGetStaticPropsType } from 'next';

type Data = {
  message: string;
  result: {
    id: string;
    films: string;
    people: string;
    planets: string;
    species: string;
    vehicles: string;
  };
};

export const getStaticProps = async () => {
  const res = await fetch('https://www.swapi.tech/api/');
  const swapis: Data = await res.json();

  return {
    props: {
      swapis,
    },
  };
};

const FilmList = ({ swapis }: InferGetStaticPropsType<typeof getStaticProps>) => {
  return (
    <>
      <h2>List of Films</h2>
      {Object.entries(swapis.result).map(([key, value]) => (
        <ul key={key}>
          <li>{value}</li>
        </ul>
      ))}
    </>
  );
};

export default FilmList;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Joris , but I got this error on screen "TypeError: Cannot convert undefined or null to object". The error on the screen it is here: "{Object.entries(swapis.result).map(([key, value]) => ("
Can you share a codesandbox and repo please? I tried on my side with my code and it works
I just did it on Sandbox. This is the link: codesandbox.io/s/crazy-minsky-cy9rg
You're not using a nextjs as expect. You might read the doc. You created the /data page to display data. You can access by its url: cy9rg.sse.codesandbox.io/data It's not recommended to import page in another one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.