I tried very hard to find a solution to this problem, but I didn't find anything, could anyone help?
I just want to fetch this data.
typeError: Cannot read property 'map' of undefined -> Error
import styles from '../styles/galery.module.css'
import Image from 'next/image'
import Link from 'next/link'
import photo from '../public/01.png'
export const getStaticProps = async () => {
const response = await fetch('https://api.github.com/orgs/rocketseat');
const data = await response.json();
return {
props: { mods: data }
}
}
const ProjectLines = ({ mods }) => {
return (
<div className={styles.categoryWrapper}>
<h4 className={styles.subTitle}>Escritório</h4>
<div className={styles.lineWrapper}>
<a className={styles.leftArrow}>❮</a>
<div className={styles.line}>
{mods.map(mod => (
<div className={styles.imageBox}>
<Image src={photo} width={400} height={200} layout="responsive" lazy="true" placeholder="blur" />
<div className={styles.linkContent}>
<span className={styles.name}>{mod.login}</span>
<Link href=""><a className={styles.link}>Veja Mais!</a></Link>
</div>
</div>
))}
</div>
<a className={styles.rightArrow}>❯</a>
</div>
</div>
);
}
export default ProjectLines;
CodePudding user response:
Yow all this comments all they do is confusing. The solution is very simple right before you map them mods check if they exists how ?
{mods && mods.map(.....
That's one way or the other is to add a default value
({ mods = []}) =>
But do the first one, is better
CodePudding user response:
well, the error is simple basically the response that you are getting is a json
object, a plain json object.
with this said, basically you can not map over a object or well, you can but not using map.
the response of the url is
{
login: "Rocketseat",
id: 28929274,
node_id: "MDEyOk9yZ2FuaXphdGlvbjI4OTI5Mjc0",
url: "https://api.github.com/orgs/Rocketseat",
repos_url: "https://api.github.com/orgs/Rocketseat/repos",
events_url: "https://api.github.com/orgs/Rocketseat/events",
hooks_url: "https://api.github.com/orgs/Rocketseat/hooks",
issues_url: "https://api.github.com/orgs/Rocketseat/issues",
members_url: "https://api.github.com/orgs/Rocketseat/members{/member}",
public_members_url: "https://api.github.com/orgs/Rocketseat/public_members{/member}",
avatar_url: "https://avatars.githubusercontent.com/u/28929274?v=4",
description: "Plataforma de educação em tecnologia