【问题标题】:TypeError: Cannot read property 'match' of undefined ( react and graphql )TypeError:无法读取未定义的属性“匹配”( react 和 graphql )
【发布时间】:2021-04-04 22:09:23
【问题描述】:

我正在尝试从 URL 中获取一部分以将其作为参数传递给 graphql,以便我可以获取数据!首先,我创建了一个 react 应用程序并从 graphql 获取数据以获取所有订单的列表,然后我创建了一个按钮,该按钮将重定向到应该显示订单详细信息的新页面

订单号是这样的:gid://shopify/Order/12345678(不仅仅是数字)

订单页面详细信息的链接如下所示:http://localhost:3000/orders/gid://shopify/Order/12345678

为了访问这个页面,我在我的 ListOrders 组件中添加了这个链接:

 <Link to={`/orders/${id}`} className="btn btn-secondary">
  Details
 </Link>

虽然我在路线上遇到了问题,但我通过制作这样的路径解决了它:

 <Route exact path='/orders/gid://shopify/Order/:id' component={OrderDetails}  />

我正在尝试在 Orderdetais 组件中以这种方式从 url 获取 ID:

 let { id } = props.match.params.id;

OrderDetails 组件:

import React from 'react';
import { gql, useQuery } from '@apollo/client';
import Table from 'react-bootstrap/Table';
import { Link } from 'react-router-dom';
const GET_Single_Orders = gql`
  query Order($id: ID!) {
    Order(id: $id) {
      id
      displayFinancialStatus
      displayFulfillmentStatus
      email
      id
      createdAt
      subtotalPrice
      totalRefunded
      totalShippingPrice
      totalPrice
      totalTax
      updatedAt
      lineItems {
        edges {
          node {
            customAttributes {
              key
              value
            }
            quantity
            title
            id
            variant {
              id
              title
              weight
              weightUnit
              price
              image {
                src
              }
            }
          }
        }
      }
      shippingAddress {
        address1
        address2
        city
        company
        country
        countryCode
        firstName
        id
        lastName
        name
        phone
        province
        provinceCode
        zip
      }
    }
  }
`;

export default function OrderDetails({ props }) {
  let { id } = props.match.params.id;
  const { loading, error, data } = useQuery(GET_Single_Orders, {
    variables: { id }
  });

  if (loading) return <h4>Loading...</h4>;
  if (error) return `Error! ${error}`;

  return (
    <div>
      <Table striped bordered responsive hover size="sm">
        <thead>
          <tr>
            <th>Created at</th>
            <th>created by</th>
            <th>Fulfillment</th>
            <th>Financial Status</th>
            <th>total Price</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          {data.Order.map(({ id, createdAt }) => (
            <tr key={id}>
              <td>{createdAt} </td>
            </tr>
          ))}
        </tbody>
      </Table>
    </div>
  );
}

我需要从 URL 获取 id (gid://shopify/Order/12345678) 并将其传递给变量,如图所示,但我收到此错误 TypeError: Cannot read property 'match' of undefined强>

更新:

此错误已更正,但我仍然无法从 graphql 获取详细信息数据,因为只有 id 中的数字作为变量发送!

更新:

<Route exact path='/orders/:gid://shopify/Order/:id' component={OrderDetails}  />

错误:TypeError: Cannot read property 'map' of null

请求中发送的变量:id:12345678(只有数字,但我需要 gid://shopify/Order/12345678

【问题讨论】:

  • 只需在传递给变量之前将字符串添加到 id(来自路由)
  • 是的,我这样做了,我得到了随请求发送的 id,但随后我得到了数据。命令。地图不是函数错误
  • 在这个“深度级别”没有数组?

标签: javascript reactjs graphql create-react-app react-apollo


【解决方案1】:

在定义props 时不应使用{}

export default function OrderDetails(props) {

更新

改变

<Route exact path='/orders/gid://shopify/Order/:id' component={OrderDetails}  />

<Route exact path='/orders/:id' component={OrderDetails}  />

当你调用 gql 查询时,像下面这样传递 id。

const { loading, error, data } = useQuery(GET_Single_Orders, {
    variables: { id: `gid://shopify/Order/${id}` }
});

【讨论】:

  • 这有点工作,但它产生了其他问题:TypeError: Cannot read property 'map' of null 可能是因为它没有得到正确的 url 所以它调用了一个错误?
  • 表示查询到的data.Order值为null。您应该检查 gql 查询是否正常。
  • 是的,当我将 id (如帖子中所示)作为查询变量传递时,它在 Graphql 中工作正常,但在这里它不起作用,所以我认为问题在于我不是获取正确的 ID
  • 我认为您的路线有错字。请检查我更新的答案,如果我错了,请告诉我。
  • @RaziMelliti 你可以console.log(id)(仅限数字?)或检查网络请求正文详细信息以检查作为变量传递/发送的内容
猜你喜欢
  • 2020-05-14
  • 2022-01-05
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2020-03-25
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多