【问题标题】:My graphQl query find returns null response on GraphQL Playground but on console there is array of find data我的 graphQl 查询查找在 GraphQL Playground 上返回空响应,但在控制台上有查找数据数组
【发布时间】:2023-01-13 15:50:47
【问题描述】:
query GetJourneys {
 getJourneyById(userId: "61dcc7de6d06a5db38d7b6e0"){
  id
  city
  trip
  tripName
  duration
  userId
}
}

因此,当我运行此查询并且我的解析器具有 const journey = await Journey.find({ userId: userId }); 时,它在所有字段中返回 null

相反,当我像 findOne 那样做时,有正确的响应,但只有 1 个对象是这样的

  {
    _id: 61de5fb2130167512ed537dc,
    city: 'Indonesia Convention Exhibition (ICE) BSD City',
    trip: 0,
    tripName: 'Indonesia Trip',
    duration: 1,
    url: 'http://www.ice-indonesia.com/',
    userId: '61dcc7de6d06a5db38d7b6e0',
    cityId: 'ChIJBSMVX1P7aS4RePSYsNhuQDQ',
    __v: 0
  }
]

仅在 GraphQL 游乐场数据返回为 null 可能是什么原因?

【问题讨论】:

    标签: mongodb graphql next.js gql graphql-playground


    【解决方案1】:

    您的 Journey.find 返回一个数组。这就是为什么它不起作用。 findOne 返回一个对象,这就是它工作的原因。

    在您的 GraphQL 查询中,您需要将返回设置为数组,只需将方括号放在您现在拥有的内容周围,它就可以工作。例如,您的类型查询中有示例类型 :Journey,只需将其更改为 :[Journey] 即可。方括号表示 Journey 数组。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2023-03-05
      • 2017-11-19
      • 2019-07-28
      • 2019-07-22
      • 2019-02-19
      • 2018-06-13
      • 2022-10-04
      • 2020-04-08
      相关资源
      最近更新 更多