paypal 检查节点 js 错误 UnhandledPromiseRejectionWarning: TypeError: 无法读取属性 'price'当我运行我的代码时未定义

发布于 2025-01-10 07:48:47 字数 1421 浏览 0 评论 0原文

我有以下代码,它假设将数据发送给 pay pal 来处理付款,但它不断收到此 TypeError: Cannot read property 'price' of undefined 我已经浏览了代码,但我认为错误正在发生“storeItems.get( item.id).price" 当我尝试获取某些原因的“items id”时,我们看不到该值,

这是我的请求正文包含以下数组

{"items":[    
    {"id":"1", "quantity":1},    
    {"id":"2", "quantity":2},  
    {"id":"3", "quantity":3},    
    {"id":"4", "quantity":4}   
]}
app.post("/create-order", async (req, res) => {
 

  const request = new paypal.orders.OrdersCreateRequest()
  const total = req.body.items.reduce((sum, item) => {
    return sum + storeItems.get(item.id).price * item.quantity
  }, 0)
  console.log(total);
  request.prefer("return=representation")
  request.requestBody({
    intent: "CAPTURE",
    purchase_units: [
      {
        amount: {
          currency_code: "USD",
          value: total,
          breakdown: {
            item_total: {
              currency_code: "USD",
              value: total,
            },
          },
        },
        items: req.body.items.map(item => {
          const storeItem = storeItems.get(item.id)
          return {
            name: storeItem.name,
            unit_amount: {
              currency_code: "USD",
              value: storeItem.price,
            },
            quantity: item.quantity,
          }
        }),
      },
    ],
  })
  
  const order = await paypalClient.execute(request) 
})

I have the following code it suppose to send the data to pay pal to process the payment but it keepsgeting this TypeError: Cannot read property 'price' of undefined i have gone through the code but i think the error is occuring "storeItems.get(item.id).price" when i try to get the "items id" for some reson thwe value is nt being seen

this is my request body contains the following array

{"items":[    
    {"id":"1", "quantity":1},    
    {"id":"2", "quantity":2},  
    {"id":"3", "quantity":3},    
    {"id":"4", "quantity":4}   
]}
app.post("/create-order", async (req, res) => {
 

  const request = new paypal.orders.OrdersCreateRequest()
  const total = req.body.items.reduce((sum, item) => {
    return sum + storeItems.get(item.id).price * item.quantity
  }, 0)
  console.log(total);
  request.prefer("return=representation")
  request.requestBody({
    intent: "CAPTURE",
    purchase_units: [
      {
        amount: {
          currency_code: "USD",
          value: total,
          breakdown: {
            item_total: {
              currency_code: "USD",
              value: total,
            },
          },
        },
        items: req.body.items.map(item => {
          const storeItem = storeItems.get(item.id)
          return {
            name: storeItem.name,
            unit_amount: {
              currency_code: "USD",
              value: storeItem.price,
            },
            quantity: item.quantity,
          }
        }),
      },
    ],
  })
  
  const order = await paypalClient.execute(request) 
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-01-17 07:48:47

该错误意味着 total 函数中的 storeItems.get(item.id) 未定义。
根据您的评论,storeItems 的定义如下:

const storeItems = new Map([
  [1, { price: 100, name: "Learn React Today" }],
  [2, { price: 200, name: "Learn CSS Today" }],
]) 

因此问题必须来自 item。它没有 id 属性,或者未定义,或者它的 id 属性与地图中的任何条目都不匹配。

The error means that storeItems.get(item.id) in the total function is undefined.
From your comment, storeItems is defined like:

const storeItems = new Map([
  [1, { price: 100, name: "Learn React Today" }],
  [2, { price: 200, name: "Learn CSS Today" }],
]) 

So the issue must come from the item. Either it does not have an id property, or it is undefined, or its id property does not match any entry in your map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文