简体   繁体   English

序列化错误 - 在 next.js 中使用 getServerSideProps

[英]Error serializing - using getServerSideProps in next.js

An error showing显示错误

Error: Error serializing .myBlogs returned from getServerSideProps in "/blog".```错误:序列化从“/blog”中的getServerSideProps返回的.myBlogs时出错。```

while I am trying to console data!当我试图控制数据时! While I was fetching data through useEffect() everything was going well, but now fetching data with getServerSideProps it throws error mentioned above!当我通过useEffect()获取数据时,一切进展顺利,但现在使用getServerSideProps获取数据时会引发上述错误!

Code:代码:

import React, { useEffect, useState } from 'react'
import styles from '../styles/Blog.module.css'
import Link from 'next/link'
import Navbar from '../components/Navbar'

const blog = (props) => {
  console.log(props)
  const [blogs, setblogs] = useState([]);
  // useEffect(() => {
  
   
  // }, [])
  return <div>
    <Navbar />
    <main className={styles.main}>
      <h2 className={styles.heading}>
        Latest Blogs :)
        {/* Get started by editing{' '}
          <code className={styles.code}>pages/index.js</code> */}
      </h2>
      <div className={styles.neat}>
        {blogs.map((blogitem) => {
          return <div className={styles.grid} key={blogitem.slug} >
            <Link href={`/blogpost/${blogitem.slug}`} >
              <a className={styles.card}>
                <h2>{blogitem.title} &rarr;</h2>
                <p>{blogitem.content.substr(0, 100)}...</p>
              </a>
            </Link>
          </div>
        })}
      </div>

    </main>
  </div>

}

export async function getServerSideProps(context) {

  let data = await fetch('http://localhost:3000/api/blogs');
  let myBlogs = await data.json();

  return {
    props: {myBlogs}, // will be passed to the page component as myBlogs
  }
}

export default blog

As @Bravo suggested, you should await the data.json() call in the getServerSideProps callback.正如@Bravo 建议的那样,您应该在getServerSideProps回调中await data.json()调用。
Also, you should destructure the props object to receive the blogs loaded and display them.此外,您应该解构props对象以接收加载的blogs并显示它们。
Finally, it is good practice in React to capitalize component names:最后,在React中将组件名称大写是一种很好的做法:

const Blog = ({ blogs }) => {
  return <div>
    <Navbar />
    <main className={styles.main}>
      <h2 className={styles.heading}>
        Latest Blogs :)
        {/* Get started by editing{' '}
          <code className={styles.code}>pages/index.js</code> */}
      </h2>
      <div className={styles.neat}>
        {blogs.map((blogitem) => {
          return <div className={styles.grid} key={blogitem.slug} >
            <Link href={`/blogpost/${blogitem.slug}`} >
              <a className={styles.card}>
                <h2>{blogitem.title} &rarr;</h2>
                <p>{blogitem.content.substr(0, 100)}...</p>
              </a>
            </Link>
          </div>
        })}
      </div>

    </main>
  </div>

}

export async function getServerSideProps(context) {

  let data = await fetch('http://localhost:3000/api/blogs');
  let myBlogs = await data.json();

  return {
    props: { blogs: myBlogs }, 
  }
}

export default Blog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM