【问题标题】:Why Displaying Error in Apollo Client in React Not Showing为什么在 React 中的 Apollo 客户端中显示错误未显示
【发布时间】:2021-10-05 04:29:13
【问题描述】:

我需要显示来自 apollo 的错误。我想知道为什么如果我 console.log 它会显示Error: undefined at new ApolloError

const {
  error,
  loading,
  refetch,
  data: { findOrders: { orders } = {} } = {},
} = useQuery(FIND_ORDERS, {
  notifyOnNetworkStatusChange: true,
  fetchPolicy: "no-cache",
  variables: {
    fromDate: "date here",
    toDate: "date here",
  },
});

if (error) {
  console.error(error);
  return <div>Error!</div>;
}

【问题讨论】:

    标签: reactjs react-hooks apollo apollo-client


    【解决方案1】:

    试试useEffect钩子

    useEffect(() => {
      if (error) {
        console.error(error);
        return <div>Error!</div>;
      }
    }, [error])
    

    在您的示例中,检查 error 不起作用,因为对象 error 尚未准备好。

    【讨论】:

    • @Joseph 所以也许你只是没有错误:D 尝试从查询中控制一个对象并检查你是否有任何响应
    • 其实有。更多关于 Apollo 客户端
    • @Joseph 我现在不知道。这取决于你在哪里运行这个查询等等。 Imo,尝试console.log 回复并确定您在data 上是否有错误。我不能帮你知道。
    猜你喜欢
    • 2019-10-05
    • 2021-12-02
    • 1970-01-01
    • 2020-03-06
    • 2016-03-31
    • 2021-12-24
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多