简体   繁体   English

来自GraphQL查询的处理数据(带有片段)响应?

[英]Process data from GraphQL query (with fragments) response?

I'm building a site with React and Gatsby, and using GraphQL to get data from our Markdown files. 我正在使用React和Gatsby构建一个站点,并使用GraphQL从我们的Markdown文件中获取数据。 For context, this problem is regarding our Team page that will organize all team members by section, so we want to render a different React component for each section ( Mission Control , Launch Team , Christchurch and Wellington ). 就上下文而言,此问题与我们的“团队”页面有关,该页面将按部分组织所有团队成员,因此我们希望为每个部分( Mission ControlLaunch TeamChristchurchWellington )呈现不同的React组件。

This is our query at the bottom of our team.jsx : 这是我们在team.jsx底部的查询:

 export const query = graphql` query teams { missionControl: allMarkdownRemark( filter: { fileAbsolutePath: { regex: "/(/content/members/missionControl)/.*\\\\.md$/" } } ) { edges { node { ...memberFields } } } wellington: allMarkdownRemark( filter: { fileAbsolutePath: { regex: "/(/content/members/wellington)/.*\\\\.md$/" } } ) { edges { node { ...memberFields } } } } fragment memberFields on MarkdownRemark { id frontmatter { title cover { childImageSharp { fluid(maxWidth: 1000, quality: 90, traceSVG: { color: "#2B2B2F" }) { base64 tracedSVG aspectRatio src srcSet srcWebp srcSetWebp sizes originalImg originalName presentationWidth presentationHeight } } } } } `; 

And here is where we're trying to use the data object: 这是我们尝试使用数据对象的地方:

 const Team = ({ data }) => { const { edges } = data.allMarkdownRemark; return ( <Layout> <Helmet title={'Team Page'} /> <Header title="Our Team"></Header> {edges.map(({ node }) => ( <MissionControlList key={node.id} title={node.frontmatter.title} cover={node.frontmatter.cover.childImageSharp.fluid} /> ))} // repeat {edges.map...} for the 3 other section lists 

Our problem is: We're currently getting a TypeError: Cannot read property 'edges' of undefined and we don't know how to access in our JSX code the different member fields for each section ( data.missionControl , data.wellington etc. didn't work) 我们的问题是:我们当前收到TypeError: Cannot read property 'edges' of undefined并且我们不知道如何在JSX代码中访问每个部分的不同成员字段( data.missionControldata.wellington等。没用)

But it says you that don't have allMarkdownRemark property on the data object. 但是它说您在数据对象上没有allMarkdownRemark属性。

This line produces that error: 此行产生该错误:

const { edges } = data.allMarkdownRemark; const {edges} = data.allMarkdownRemark;

Try just to console.log your data. 尝试仅console.log您的数据。 You'll see that you have properties { missionControl: {}, wellington: {} } because you created GraphQL query with aliases, take a look at the docs 您会看到您具有属性{ missionControl: {}, wellington: {} }因为您使用别名创建了GraphQL查询,请查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM