GraphQL不返回错误,但未定义

GraphQL不返回错误,但未定义,graphql,gatsby,Graphql,Gatsby,我已经找了一段时间了。我使用的是盖茨比,控制台或终端中没有错误,但值未定义。可能是因为文章中的第二个数据吗 GraphiQL查询: 查询/组件代码: import React, { Component } from 'react' import { graphql } from 'gatsby' // This query is executed at build time by Gatsby. export const GatsbyQuery = graphql` { umdHu

我已经找了一段时间了。我使用的是盖茨比,控制台或终端中没有错误,但值未定义。可能是因为文章中的第二个数据吗

GraphiQL查询:

查询/组件代码:

import React, { Component } from 'react'
import { graphql } from 'gatsby'

// This query is executed at build time by Gatsby.
export const GatsbyQuery = graphql`
  {
    umdHub {
      articles {
        data {
          title
        }
      }
    }
  }
`

class ClientFetchingExample extends Component {
  render() {
    const {
      umdHub: { articles },
    } = this.props.data

    console.log(articles.title)
    return (
      <div style={{ textAlign: 'center', width: '600px', margin: '50px auto' }}>
        <h1>{articles.title} - Is the title of the article</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      </div>
    )
  }
}

export default ClientFetchingExample

你已经走到一半了,只是一些小的变化

const {
      umdHub: { articles },
    } = this.props.data
现在你有了这样的物品

articles: {
    data: [
        { 
          title: 'Learning to Listen'
        },
        { 
          title: 'Second Title'
        }
        ...
    ]
}
现在您需要对数据进行循环

import _ from 'lodash'
const data = _.get(articles, 'data', [])

_.map(data, title => {
    console.log({ title })
})
如果你只想获得第一个头衔的话

const title = _.get(articles, 'data[0].title', 'default value or blank string')

lodash是一个提供模块化、性能和附加功能的现代JavaScript实用程序库。

不清楚您使用什么插件将umdHub添加到graphql模式。你能不能给插件添加链接或者添加gatsby config.js?^ umdHub可能是问题所在?你没有在graphiq编辑器中使用它?另外,从graphql来看,文章似乎包含一个数据对象,其中包含一个标题数组,但是您正在访问articles.title。也许可以尝试articles.data[0]。titleI正在使用gatsby source graphql,umdHub是我的字段名在graphql游乐场中进行查询吗?我认为articles返回的是数组而不是对象,因此您必须控制台。logarticles;相反,然后对它们进行迭代。