【问题标题】:How do I wait for a query to finish before making another query with graphql?在使用 graphql 进行另一个查询之前,如何等待查询完成?
【发布时间】:2019-06-29 03:47:41
【问题描述】:

我有一个名为 GET_ME 的查询和另一个名为 GET_USER_NOTIFICATIONS 的查询。第二个它正在寻找将来自第一个查询的用户 ID。我的问题是有时我会收到[TypeError: undefined is not an object (evaluating 'o.props.getMe.me._id')]

这是我的代码:

export default compose(
  withApollo,
  graphql(GET_ME, { name: "getMe" }),
  graphql(GET_USER_NOTIFICATIONS, {
    name: "notification",
    skip: props => !props.getMe || !props.getMe.me,
    options: props => ({
      variables: { r_id: props.getMe.me._id }
    })
  })
)(Notifications);

有什么帮助吗?

【问题讨论】:

    标签: reactjs react-native graph apollo react-apollo


    【解决方案1】:

    你能试试吗:

    variables: { r_id: props.getMe && props.getMe.me ? props.getMe.me._id : null }
    // I use "null" in case the object doesn't exists. 
    // You can use value that your graphql needs.
    

    (为简单起见,您也可以使用 lodash _.get(...))

    该错误表明它正在执行变量赋值,尽管您已将查询设置为跳过。但是在分配时,getMe 或 getMe.me 是“未定义的”。

    如果这没有帮助,您可以在 render 方法中使用 Render props Apollo 组件。这样,您就可以保证在没有访问getMe.me._id 的情况下无法设置变量。

    【讨论】:

    • 非常感谢哥们:)
    猜你喜欢
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多