【发布时间】:2021-02-05 17:30:13
【问题描述】:
我正在尝试查询特定类别标题并将其返回到 div 中。它给了我以下错误信息:
TypeError: can't access property "node", data.edges is undefined
这是我的文件:
import React from "react"
import { graphql, useStaticQuery } from "gatsby"
import styled from "styled-components"
import { H3, BodyMain } from "../styles/TextStyles"
export default function CategorySection() {
const data = useStaticQuery(graphql`
query categoryQuery {
allGraphCmsCategory(filter: { title: { eq: "CSS" } }) {
edges {
node {
title
slug
description
}
}
}
}
`)
return (
<Wrapper>
<ContentWrapper>
<TextWrapper>
<Title>Browse by Categories</Title>
<Description>
Use the category tags to narrow down what you are looking for.
</Description>
</TextWrapper>
<CategoryWrapper>
<Categories>{data.edges.node.title}</Categories>
</CategoryWrapper>
</ContentWrapper>
</Wrapper>
)
}
const Wrapper = styled.div``
const ContentWrapper = styled.div``
const TextWrapper = styled.div``
const Title = styled(H3)``
const Description = styled(BodyMain)``
const CategoryWrapper = styled.div``
const Categories = styled.div``
我相信我的查询是正确的,因为我可以在 http://localhost:8000/___graphql 上看到结果
当我对其进行测试并看到它工作时,我想映射所有类别并为每个类别创建单独的页面。
你能指引我正确的方向吗?
【问题讨论】:
-
使用
console.log。 @Ferran 的回答是正确的。 -
感谢您的确认。正如您在 cmets 中看到的那样,它仍然有点欺骗我。
-
嗯,边是一个数组...试试`{data.allGraphCmsCategory.edges[0].node.title}
-
好了。谢谢!
-
你应该在
console.log(data)之后看到