我正在使用 Sanity.io 并使用 GROQ 查询来为我的应用程序获取数据。我正在尝试从我的主文档类型中检索 WhyCoseExamples 字段,但它返回未定义。
schema.ts
export const mainPage = {
name: 'main',
title: 'Strona główna',
type: 'document',
fields: [
{
name: 'header',
title: 'Nagłówek',
type: 'string',
},
{
name: 'subheader',
title: 'Podtytuł',
type: 'string',
},
{
name: 'buttonText',
title: 'Text na przycisku',
type: 'string',
},
{
name: 'backgroundImage',
title: 'Obraz tła',
type: 'image',
options: { hotspot: true },
fields: [{ name: 'alt', title: 'Tekst alternatywny (SEO)', type: 'string' }],
},
{
name: 'whyChose',
title: 'Dlaczego warto nas wybrać',
type: 'string',
},
{
name: 'whyChoseTitle',
title: 'Dlaczego warto nas wybrać - Tytuł',
type: 'string',
},
{
name: 'whyChoseSubtitle',
title: 'Dlaczego warto nas wybrać - Podtytuł',
type: 'string',
},
{
name: 'whyChoseExamples',
title: 'Dlaczego warto nas wybrać - Zalety współpracy',
type: 'array',
of: [
{
type: 'object',
name: 'textWithImage',
title: 'Karta zalet',
fields: [
{
name: 'header',
title: 'Tytuł',
type: 'string',
},
{
name: 'subHeader',
title: 'Podtytuł',
type: 'string',
},
{
name: 'image',
title: 'Image',
type: 'image',
options: {
hotspot: true,
},
fields: [
{
name: 'alt',
title: 'Tekst alternatywny (SEO)',
type: 'string',
},
],
},
],
},
],
},
],
};
groq query
export const getMainPageContent = async (): Promise<MainPage[]> => {
const client = createClient({
projectId: 'myprojectId',
dataset: 'production',
apiVersion: '2024-09-17',
});
return client.fetch(groq`
*[_type == "main"]{
header,
subheader,
buttonText,
backgroundImage {
asset->{
_id,
url
},
alt,
},
whyChose,
whyChoseTitle,
whyChoseSubtitle,
whyChoseExamples{
header,
subHeader,
image {
asset->{
_id,
url
},
alt,
}
},
}
`);
};
当我运行此查询并尝试访问 WhyCoseExamples 时,它返回为未定义。
我尝试过的:
检查的字段名称:
确保查询中的所有字段名称与我的 Sanity 模式中的字段名称完全匹配。 已验证是whyChooseExamples,而不是whyChooseExamples。 确认数据输入:
在 Sanity Studio 中,whyCoseExamples 字段填充有我的主文档中的数据。
使用 Sanity 的视觉工具:
在 Sanity Studio 内的 Vision 工具中运行查询。 WhyCoseExamples 字段确实出现在结果中。
附加信息:
其他字段(例如 header、subheader 和 WhyCose)均已正确返回。 没有条件字段或权限集会隐藏 WhyCoseExamples。 我正在使用最新版本的 Sanity 和 Next.js
什么可能导致 WhyCoseExamples 字段在我的 GROQ 查询中返回未定义,即使它已填充在 Sanity Studio 中并在架构中正确定义?如何修改我的查询或架构以成功检索此字段?
任何帮助将不胜感激!
我需要将其替换为
whyChoseExamples
,而不是 whyChoseExamples[]
,因为它是数组类型。