【问题标题】:Property 'me' does not exist on type '{}'.ts(2339)类型“{}”.ts(2339) 上不存在属性“我”
【发布时间】:2021-01-10 23:16:00
【问题描述】:

所以我在props.me.id 上遇到了上述错误。我该如何解决这个问题?

组件/OrderList.tsx

const USER_ORDERS_QUERY = gql`
  query USER_ORDERS_QUERY($userId: String) {
    orders(where: { user: { equals: $userId } }, orderBy: { createdAt: desc }) {
      id
      total
      createdAt
      items {
        id
        title
        price
        description
        mainDescription
        quantity
        image
      }
      user
    }
  }
`;

const userOrdersQuery = graphql(
  USER_ORDERS_QUERY,
  {
    options: props => ({
      variables: {
        userId: props.me.id,
      },
      fetchPolicy: 'cache-and-network',
      pollInterval: 300
    })
  }
);

const OrderList = props => {
  const { me, user_ip, user_Agent, url, urlReferer, client, data: { orders, loading: loadingQuery, error: errorQuery, startPolling, stopPolling, subscribeToMore }} = props;

}

const ItemWithApollo = withApollo(OrderList)
export default memo(userOrdersQuery(ItemWithApollo));

我的部分回购可以在这里找到:https://github.com/TheoMer/next_apollo

【问题讨论】:

    标签: typescript graphql apollo-client


    【解决方案1】:

    您尚未为 props 指定类型。

    在许多情况下,TypeScript 无法自动推断参数的类型,您通常需要手动指定它们(如果可能)或使用 props: any(如果不是 - 但如果可以的话,请避免这样做!)。

    在这种情况下,您只需指定props.me.id 属性,如下所示:

    options: (props: { me: { id: string } }) => ({
      // ...
    

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 2021-08-02
      • 2020-06-02
      • 2022-07-09
      • 2022-10-04
      • 2023-02-07
      • 2020-08-23
      • 2021-12-19
      • 2020-07-16
      相关资源
      最近更新 更多