访问分组graphql查询中的Frontmatter元素
我试图从类别中最新帖子的frontmatter中返回featuredImage 以下是我目前编写的graphql查询:访问分组graphql查询中的Frontmatter元素,graphql,gatsby,sharp,Graphql,Gatsby,Sharp,我试图从类别中最新帖子的frontmatter中返回featuredImage 以下是我目前编写的graphql查询: query catList { allMdx(sort: { fields: frontmatter___date, order: DESC }) { group(field: frontmatter___themes, limit: 1) { fieldValue totalCount
query catList {
allMdx(sort: { fields: frontmatter___date, order: DESC }) {
group(field: frontmatter___themes, limit: 1) {
fieldValue
totalCount
nodes {
frontmatter {
featuredImage {
childImageSharp {
fixed(width: 400, height: 400) {
...GatsbyImageSharpFixed
}
}
}
}
}
}
}
}
查询返回的正是我在graphql编辑器中想要的内容,但是当我尝试访问组件中的featuredImage时,出现了一个错误:
“TypeError:无法读取未定义的属性'featuredImage'”
以下是页面和组件的完整代码:
const CatPage = () => (
<StaticQuery
query={graphql`
query themeList {
allMdx(sort: { fields: frontmatter___date, order: DESC }) {
group(field: frontmatter___themes, limit: 1) {
fieldValue
totalCount
nodes {
frontmatter {
featuredImage {
childImageSharp {
fixed(width: 400, height: 400) {
...GatsbyImageSharpFixed
}
}
}
}
}
}
}
}
`}
render={data => (
<Layout>
<Gallery>
{data.allMdx.group.map(theme => (
<GalleryCard to={theme.fieldValue} title={theme.fieldValue}>
<Img
fixed={
theme.nodes.frontmatter.featuredImage.childImageSharp.fixed
}
/>
</GalleryCard>
))}
</Gallery>
</Layout>
)}
/>
)
const CatPage=()=>(
(
{data.allMdx.group.map(主题=>(
))}
)}
/>
)
这里的目标是让每个类别的最新文章中的特色图像成为该类别的封面图像,但我很难获得图像渲染 所有的帖子都有frontmatter吗?是的,整个项目中的所有MDX文件都有frontmatter。我现在正在测试3个文件。