【问题标题】:How to use data from GraphQL in a React component in GatsbyJS如何在 GatsbyJS 的 React 组件中使用来自 GraphQL 的数据
【发布时间】: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)之后看到

标签: graphql gatsby


【解决方案1】:

您的查询看起来不错,但是,您需要访问嵌套对象,如 GraphQL 所示,在您的情况下,这应该可以工作:

export default function CategorySection() {
  const data = useStaticQuery(graphql`
    query categoryQuery {
      allGraphCmsCategory(filter: { title: { eq: "CSS" } }) {
        edges {
          node {
            title
            slug
            description
          }
        }
      }
    }
  `)

 console.log("your data is", data.allGraphCmsCategory.edges) // use to access to the nested data data.allGraphCmsCategory.edges[0].node.title
  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>
  )
}

请注意,在内部数据中,您首先需要访问allGraphCmsCategory 并继续跟踪对象树。我假设(因为allGraphCmsCategory 中的all 关键字)结果将有多个边节点(数组),这就是edges[0] 的原因。

或者,您可以使用 StaticVersion 组件:

    export default function CategorySection() {
       return (
    <StaticQuery
      query={graphql`
        query categoryQuery {
          allGraphCmsCategory(filter: { title: { eq: "CSS" } }) {
            edges {
              node {
                title
                slug
                description
              }
            }
          }
        }
      `}
      render={data => {
       console.log(data);
       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.allGraphCmsCategory.edges[0].node.title}</Categories>
            </CategoryWrapper>
          </ContentWrapper>
        </Wrapper>
      )
      }}
    />
  )
    }

使其动态化:

{data.allGraphCmsCategory.edges.map(item=>{
return <Categories>{item.title}</Categories>
})}

【讨论】:

  • 我试图实现console.log,但我一定做错了。它现在给了我这个错误:“TypeError: can't access property 0, data.allGraphCmsCategory.edges.node is undefined”
  • 我不确定它是否与从 GraphCMS 获取数据有关。如果我对本地文件执行 useStaticQuery,它似乎工作正常。
  • 是的,这就是为什么我说我假设了一些东西,但不知道你的数据结构是不可能的。把控制台改成console.log(data.allGraphCmsCategory.edges)查看输出
  • 是的,这是有道理的。奇怪的是,我可以使用 localhost:8000/___graphql 进行相同的查询并获得结果,但在 useStaticQuery 中使用它会给我未定义的结果。
  • data 总是未定义吗? console.log(data)
猜你喜欢
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 2018-05-02
  • 2019-10-20
  • 2018-11-28
  • 2022-08-14
  • 2019-06-22
  • 2019-08-04
相关资源
最近更新 更多