paypal 检查节点 js 错误 UnhandledPromiseRejectionWarning: TypeError: 无法读取属性 'price'当我运行我的代码时未定义
我有以下代码,它假设将数据发送给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误意味着
total
函数中的storeItems.get(item.id)
未定义。根据您的评论,storeItems 的定义如下:
因此问题必须来自
item
。它没有id
属性,或者未定义,或者它的id
属性与地图中的任何条目都不匹配。The error means that
storeItems.get(item.id)
in thetotal
function is undefined.From your comment, storeItems is defined like:
So the issue must come from the
item
. Either it does not have anid
property, or it is undefined, or itsid
property does not match any entry in your map.