为啥在 React 中的 Apollo 客户端中显示错误未显示

Posted

技术标签:

【中文标题】为啥在 React 中的 Apollo 客户端中显示错误未显示【英文标题】: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>;

【问题讨论】:

【参考方案1】:

试试useEffect钩子

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

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

【讨论】:

@Joseph 所以也许你只是没有错误:D 尝试从查询中控制一个对象并检查你是否有任何响应 其实有。更多关于 Apollo 客户端 @Joseph 我现在不知道。这取决于你在哪里运行这个查询等等。 Imo,尝试console.log 回复并确定您在data 上是否有错误。我不能帮你知道。

以上是关于为啥在 React 中的 Apollo 客户端中显示错误未显示的主要内容,如果未能解决你的问题,请参考以下文章