blogs.map不是next.js应用中的函数错误

发布于 2025-01-28 10:02:15 字数 2058 浏览 3 评论 0原文

import Head from 'next/head'
import { useState } from 'react'
import Image from 'next/image'
import styles from '../styles/Home.module.css'

const Home = (props) => {
  const [blogs, setblogs] = useState(props.data);

  return <div className={styles.container}>
    <Head>
      <title>BlogsWap</title>
      <meta name="description" content="New App" />
      <link rel="icon" href="/favicon.ico" />
    </Head>

    <main className={styles.main}>
      <h1 className={styles.title}>
        Welcome to <a href="https://nextjs.org">BlogsWap!</a>
      </h1>

      <p className={styles.description}>
        An all time blog for coders!
      </p>
      {blogs.map((blogitem) => {
        return <div className={styles.grid}>
          <a href="https://nextjs.org/docs" className={styles.card}>
            <h2>{blogitem.title} &rarr;</h2>
            <p>{blogitem.content}</p>
          </a>

        </div>
      })}

    </main>

    <footer className={styles.footer}>
      <a
        href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
        target="_blank"
        rel="noopener noreferrer"
      >
        &copy; BlogsWap
        <span className={styles.logo}>
        </span>
      </a>
    </footer>
  </div>

}
export async function getServerSideProps(context) {
  let totalBlogs = await fetch('http://localhost:3000/api/blogs');
  // console.log(totalBlogs)
  let data = await totalBlogs.json();
  return {
    props: { data }, // will be passed to the page component as props
  }
}

export default Home

我的错误是:

typeerror:blogs.map不是函数

我该怎么办?我不知道为什么会出现,因为 最近我使用了类似的方法,但一切都很好 现在,它引发了这样的错误! 请帮助我进入错误!

这是错误的照片:(

发生了错误的照片

import Head from 'next/head'
import { useState } from 'react'
import Image from 'next/image'
import styles from '../styles/Home.module.css'

const Home = (props) => {
  const [blogs, setblogs] = useState(props.data);

  return <div className={styles.container}>
    <Head>
      <title>BlogsWap</title>
      <meta name="description" content="New App" />
      <link rel="icon" href="/favicon.ico" />
    </Head>

    <main className={styles.main}>
      <h1 className={styles.title}>
        Welcome to <a href="https://nextjs.org">BlogsWap!</a>
      </h1>

      <p className={styles.description}>
        An all time blog for coders!
      </p>
      {blogs.map((blogitem) => {
        return <div className={styles.grid}>
          <a href="https://nextjs.org/docs" className={styles.card}>
            <h2>{blogitem.title} →</h2>
            <p>{blogitem.content}</p>
          </a>

        </div>
      })}

    </main>

    <footer className={styles.footer}>
      <a
        href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
        target="_blank"
        rel="noopener noreferrer"
      >
        © BlogsWap
        <span className={styles.logo}>
        </span>
      </a>
    </footer>
  </div>

}
export async function getServerSideProps(context) {
  let totalBlogs = await fetch('http://localhost:3000/api/blogs');
  // console.log(totalBlogs)
  let data = await totalBlogs.json();
  return {
    props: { data }, // will be passed to the page component as props
  }
}

export default Home

I am getting error like:

TypeError: blogs.map is not a function

What should I do? I have no idea why it appeared because,
recently I have used similar method but it all was fine
now it throwing such error!
Please help me in to get the error out!

Here is the photo of the error :(

Photo of the error occured

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城半夏 2025-02-04 10:02:15

您收到的错误意味着Blogs对象是未定义的,因此,blogs.map也是未定义的,在调用时会引起错误。

尝试使用可选链接:

{blogs?.map((blogitem) => {
...

The error you are getting means that the blogs object is undefined and thus, blogs.map is also undefined, raising an error when being called.

Try using optional chaining:

{blogs?.map((blogitem) => {
...
2025-02-04 10:02:15

访问博客的方式,您需要将博客作为关键传递:

 let blogs = await totalBlogs.json();
 return {
    props: { data : { blogs } }, // will be passed to the page component as props
  }

或者您可以做到这一点:

const [blogs, setblogs] = useState(props);

The way you are accessing blogs, you need to pass blogs as a key:

 let blogs = await totalBlogs.json();
 return {
    props: { data : { blogs } }, // will be passed to the page component as props
  }

Or you can do this:

const [blogs, setblogs] = useState(props);
何处潇湘 2025-02-04 10:02:15

更改getServersideProps,返回有意义的东西,实际上代表您的数据,而不是像数据这样的关键字,它令人困惑。
通过返回{props:{blogdata}},该组件将接收blogdata作为prop。

  let blogData = await totalBlogs.json();
  return {
    props: { blogData }, // will be passed to the page component as props
  }

然后,您需要更改通过道具的组件的输入,现在是blogdata。注意这里的更改,这里正在使用破坏性。

const Home = ({ blogData }) => 
const [blogs, setblogs] = useState(blogData);

最后,在JSX中还进行无效检查以安全访问对象

 {blogs && ....
   <map goes here>
 }

,或者

 {blogs ? 
    <map goes here>
    :
    else show a loader here

我希望blogdata是一个数组类型,如果不使用

Array.from(blogData)

.map仅在数组上使用,则 使用该数组类型。

Change the getServerSideProps like this, return something meaningful which actually represents your data, not a keyword like data, it's confusing.
By returning { props: { blogData } }, the component will receive blogData as a prop.

  let blogData = await totalBlogs.json();
  return {
    props: { blogData }, // will be passed to the page component as props
  }

Then you need to change the input of component where you are passing props, it's now blogData. Notice the change here, destructuring is being used here.

const Home = ({ blogData }) => 
const [blogs, setblogs] = useState(blogData);

Finally in the jsx also do null check to safely access object

 {blogs && ....
   <map goes here>
 }

Or

 {blogs ? 
    <map goes here>
    :
    else show a loader here

Also I hope blogData is an array type, if not convert it to array using

Array.from(blogData)

because .map only works on array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文