Cannot read properties of undefined (reading 'map') Next 13 Using getStaticProps

huangapple go评论66阅读模式
英文:

Cannot read properties of undefined (reading 'map') Next 13 Using getStaticProps

问题

你缺少了对返回的数据进行类型转换。在 getStaticProps 函数中,你可以将返回的数据显式地指定为一个包含 name 字段的数组。这样,TypeScript 就知道 posts 是一个数组,可以在组件中进行映射。

你可以在 getStaticProps 中这样修改:

const posts = await res.json() as { name: string }[];

这样你的代码就能正常运行了。

英文:

I was trying to fetch my data from github using getStaticProps for more performance and it returns this error even when I do a "console.log" it returns me the array but I can't map it so what am I missing

code:

"use client";

import { GetStaticProps } from "next";

interface PostGithubProps {
  posts: {
    name: string
  }[]
}

export default function Github({ posts }: PostGithubProps) {
  return(
    <>
     {posts.map(post => {
       <p key={post.name}>{post.name}</p>
      })}
    </>
  )
}

export const getStaticProps: GetStaticProps = async () => {
  const res = await fetch('https://api.github.com/users/{myuser}/repos')
  const posts = await res.json()

  return { 
    props: {
      posts
    }
  }
} 

So what am I missing to do this map works, as I already said on console return the array of repos from github.

答案1

得分: 0

因为在初始渲染时您的帖子尚不可用所以您必须先检查未定义的值类似于这样

import { GetStaticProps } from "next";

interface PostGithubProps {
  posts: {
    name: string
  }[]
}

export default function Github({ posts }: PostGithubProps) {
  if(!posts) return <div>Loading...</div>;
  return(
    <>
      {posts.map(post => {
        <p key={post.name}>{post.name}</p>
      })}
    </>
  )
}

export const getStaticProps: GetStaticProps = async () => {
  const res = await fetch('https://api.github.com/users/{myuser}/repos')
  const posts = await res.json()

  return { 
    props: {
      posts
    }
  }
}
英文:

It is because, on the initial render, your posts are not available so you must check for undefined value first something like this :

import { GetStaticProps } from &quot;next&quot;;

interface PostGithubProps {
  posts: {
    name: string
  }[]
}

export default function Github({ posts }: PostGithubProps) {
  if(!posts) return &lt;div&gt;Loading...&lt;/div&gt;;
  return(
    &lt;&gt;
     {posts.map(post =&gt; {
       &lt;p key={post.name}&gt;{post.name}&lt;/p&gt;
      })}
    &lt;/&gt;
  )
}

export const getStaticProps: GetStaticProps = async () =&gt; {
  const res = await fetch(&#39;https://api.github.com/users/{myuser}/repos&#39;)
  const posts = await res.json()

  return { 
    props: {
      posts
    }
  }
} 

答案2

得分: 0

在这个新版本的 Next.js 中,我们不再需要使用 getStaticProps 了,这是我的错误 😅。
我像这样解决了它:

interface PostGithubProps {
  name: string
}

export default async function Github() {
  const response = await fetch(`https://api.github.com/users/{我的用户名}/repos`, {
    next: {
      revalidate: 60 * 60 * 24, // 24小时
    },
  })

  const repositories = await response.json()

  return (
      {repositories.map((repos: PostGithubProps) => (
        <p key={repos.name}>
          {repos.name}
        </p>
       ))}
  )
}
英文:

In this new version of Next we don't need getStaticProps anymore my mistake 😅.
I solve it like this:

interface PostGithubProps {
  name: string
}

export default async function Github() {
  const response = await fetch(`https://api.github.com/users/{myuser}/repos`, {
    next: {
      revalidate: 60 * 60 * 24, // 24 hours
    },
  })

  const repositories = await response.json()

  return (
      {repositories.map((repos: PostGithubProps) =&gt; (
        &lt;p key={repos.name}&gt;
          {repos.name}
        &lt;/p&gt;
       ))}
  )
}

huangapple
  • 本文由 发表于 2023年4月17日 00:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定