将理智内容湖连接到前端时错误

发布于 2025-02-09 12:04:15 字数 963 浏览 1 评论 0原文

我在从理智到模板中获取博客时会遇到错误,我正在创建next.js网站

错误:错误:未知块类型“未定义”,请指定一个 serializers.types prop

中的序列化器
<PortableText
  // Pass in block content straight from Sanity.io
  content={blogs[0].content}
  projectId="oeqragbg"
  dataset="production"
  // Optionally override marks, decorators, blocks, etc. in a flat
  // structure without doing any gymnastics
  serializers = {{
    h1: (props) => <h1 style={{ color: "red" }} {...props} />,
    li: ({ children }) => <li className="special-list-item">{children}</li>,
  }}
/>

export async function getServerSideProps(context) {
  const client = createClient({
    projectId: 'oeqragbg',
    dataset: 'production',
    useCdn: false
  });
 
  const query = '*[_type == "blog"]';
  const blogs = await client.fetch(query);
  return {
    props: {
      blogs
    }
  }
}

I'm getting an error while fetching blogs from sanity to my template, I'm creating a next.js website

Error: Error: Unknown block type "undefined", please specify a
serializer for it in the serializers.types prop

<PortableText
  // Pass in block content straight from Sanity.io
  content={blogs[0].content}
  projectId="oeqragbg"
  dataset="production"
  // Optionally override marks, decorators, blocks, etc. in a flat
  // structure without doing any gymnastics
  serializers = {{
    h1: (props) => <h1 style={{ color: "red" }} {...props} />,
    li: ({ children }) => <li className="special-list-item">{children}</li>,
  }}
/>

export async function getServerSideProps(context) {
  const client = createClient({
    projectId: 'oeqragbg',
    dataset: 'production',
    useCdn: false
  });
 
  const query = '*[_type == "blog"]';
  const blogs = await client.fetch(query);
  return {
    props: {
      blogs
    }
  }
}

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

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

发布评论

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

评论(1

苦妄 2025-02-16 12:04:15

您是否正在使用@portabletext/react 软件包?
这就是您的使用方式:

import {PortableText} from '@portabletext/react'

const myPortableTextComponents = {
  block: {
    h1: ({children}) => <h1 style={{ color: "red" }}">{children}</h1>
  },
  listItem: {
    bullet: ({children}) => <li className="special-list-item">{children}</li>,
  },
}

<PortableText
  value={blogs[0].content}
  components={myPortableTextComponents}
/>

Are you using the @portabletext/react package?
This is how you would use it:

import {PortableText} from '@portabletext/react'

const myPortableTextComponents = {
  block: {
    h1: ({children}) => <h1 style={{ color: "red" }}">{children}</h1>
  },
  listItem: {
    bullet: ({children}) => <li className="special-list-item">{children}</li>,
  },
}

<PortableText
  value={blogs[0].content}
  components={myPortableTextComponents}
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文