在 SANITY 中尝试检索时无法识别(未定义)数据属性

问题描述 投票:0回答:1

我是使用 Sanity CMS 的新手。我正在尝试从我的网站项目访问名为 sanity 服务器本地标题的特定数据。

来自 SANITY SIDE PROJECT 中“article.ts”文件的一堆数据正确显示在 WEBSITE SIDE PROJECT 上,除了一个数据:

SANITY 项目方

第 1 步 |修改属性

schemaTypesarticle.ts

defineField({
      name: 'h1',
      title: 'Title',
      type: 'string',
    }),

此属性在 SANITY 端本地服务器项目中声明,我尝试将其重命名为:

defineField({
      name: 'maintitle',
      title: 'Title',
      type: 'string',
    }), 

这是我对文件进行的检查和更新:

第 2 步 |更新类型架构渲染

更新 sanity.types.ts 架构语句 DATA 的快捷命令。

//package.json 文件

"scripts": {
...
"generate": "sanity schema extract && sanity typegen generate",
...
},
npm run generate

网站项目方:

第 3 步 |将以前的架构类型数据替换为最新的架构类型数据

ypes\sanity\sanity.types.ts

删除了以前的架构数据“sanity.types.ts”(具有 h1 属性的版本),替换为最新的“sanity.types.ts”(具有 maintitle 属性的版本),来自 SANITY SIDE PROJECT 中的生成启动命令.

第 4 步 |检索数据已更新

//src\pages log[slug]

从客户端项目“slug”文件中获取数据

将“h1”更改为“maintitle”。

export async function getStaticProps(context: { params: { slug: string } }) {
  const { slug } = context.params
  const article: Article = await sanityClient.fetch(
    `*[_type == "article" && slug.current == "${slug}"][0]{
    _id,
    date,
    maintitle,
    slug,
    categories[]->,
    image,
    intro,
  }`
  )

  return {
    props: {
      article,
    },
  }
}

类型\理智文章.ts

更新“h1”->“maintitle”

export type Article = {
  author: Array<Author>
  category: Array<string>
  data: string
  description: string
  maintitle: string
  image: Image
  slug: Slug
  ...
}

然后,当我尝试执行 console.log(article.maintitle) 时,为什么我会返回 null(未定义)?

src\页面日志[slug]

...some code...
export async function getStaticProps(...) {
...
}

export default function Index(props: { article: Article }) {
  const { article } = props
  //article.intro![0].children[0].text
  //console.log(
  //  article.intro
  //    ? article.intro[0].children.map(
  //        (props: any, index: number) => (props, index)
  //      )
  //    : null
  //)
  //console.log(article.intro ? article.intro : null)
  console.log(article ? article : null)

  return (
    <div>
      <div className="flex flex-row">
        <div className="flex bg-white sm:max-w-xs"></div>

        <div>
          {article.maintitle ? article.maintitle : null}
          {article.intro ? <RichText value={article.intro} /> : null}
          {article.date ? article.date : null}
          {article.author ? <Author data={article.author} /> : null}
          <div className="flex flex-row">
            {article.categories
              ? article.categories.map((e, idx) => (
                  <Category data={e} key={idx} />
                ))
              : null}
          </div>
        </div>
      </div>
    </div>
  )
}

我错过了更新/更改 h1 属性的哪个文件?

知道该属性是一个字符串而不是一个 blockContent(因此不能从 portableText 中检索)。

欢迎任何帮助。

所有其他属性都显示良好。

reactjs next.js sanity
1个回答
0
投票

在 SANITY 中,尽管中级开发人员易于使用和实施,但在连续的步骤中很容易迷失方向,从而获得我们所搜索的结果。

我终于解决了困扰我2天多的问题。

第一步 | SANITY STUDIO(生产模式)更新未应用

很明显,如果您在 SANITY 工作室(生产预览模式)中遇到 SchemaType 自动状态更新停止的问题,您将不得不等待很长时间才能看到任何更改。

手动更新您使用此命令所做的属性更改:

npm run build | sanity build

将允许您将数据修改/添加推送到生产中部署的工作室。

第二步|文档状态创建“版本”

当您在第一个基于默认模型版本(生产预览模式 | Sanity Studio)中创建新文章时,该模型将采用第一个 SANITY 版本的默认属性,在第一次使用时未修改。

您需要删除您创建的第一个文档,该文档停留在默认属性中。

然后,要访问 SANITY 服务器的最新版本架构中添加的新属性,您需要创建一个新的“文章”。

您将轻松发现您添加的最新属性。

© www.soinside.com 2019 - 2024. All rights reserved.