Next.js14+Sanity中GROQ查询whyChoseExamples返回undefined
问题:Sanity + Next.js 14中GROQ查询whyChoseExamples字段返回undefined
我正在使用Sanity.io与Next.js 14开发应用,通过GROQ查询获取main文档类型的数据时,whyChoseExamples字段始终返回undefined。
相关代码
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查询代码
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, } }, } `); };
已完成的排查
- 确认字段名称与schema完全匹配;
- Sanity Studio中该字段已填充数据;
- 在Sanity Vision工具中执行相同查询,该字段能正常返回结果;
- 其他字段可正常获取,无隐藏该字段的权限或条件设置,使用最新版Sanity与Next.js。
请问问题原因是什么?如何修改查询或schema解决?
内容的提问来源于stack exchange,提问作者RemediumJeżozwierza
相关产品推荐
相关产品推荐

