【发布时间】:2021-03-26 08:59:24
【问题描述】:
我对节点 js 很陌生,我收到以下错误: 无法读取未定义的属性“价格”
我用过这个包:https://github.com/paypal/PayPal-node-SDK
代码无需行
amount = req.body.price;
但我需要发布数据
代码如下:
var paypal = require('paypal-rest-sdk');
var express = require('express');
var app = express();
var amount = 5;
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'AV-UFzoT7Ccua-ubSQwUGx96qVq46ySVLTHGPyMiK4CA6HP2gHNW61-cvN__sIoyxQD-xX9zZupCNi',
'client_secret': 'ELAElfR-2pjX5PWJpW3iCW0Yd-WQ_0u2LUk3BDO6v6dcSHgYv1mJG3wg6_gWaR3IwGnVvrZ8pFoRz-'
});
app.post('/pay',(req,res)=>{
amount = req.body.price;
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:8000/success",
"cancel_url": "http://localhost:8000/pay"
},
"transactions": [{
"item_list": {
"items": [{
"name": "item",
"sku": "item",
"price": amount,
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": amount
},
"description": "This is the payment description."
}]
};
paypal.payment.create(create_payment_json, (error, payment)=> {
if (error) {
throw error;
} else {
console.log(payment);
for (let i = 0; i < payment.links.length; i++) {
if (payment.links[i].rel == 'approval_url') {
console.log(payment.links[i].href);
res.redirect(payment.links[i].href);
}
}
}
});
})
提前谢谢你
另外,这就是我在 dart 中发布参数价格的方式:
String _loadHTML() {
return '''
<html>
<body onload="document.f.submit();">
<form id="f" name="f" method="post" action="http://10.0.2.2:8000/pay">
<input type="hidden" name="ok" value="$price" />
</form>
</body>
</html>
''';
}
【问题讨论】:
-
你向服务器发送什么请求?它有身体吗?此外,您还缺少 express 的正文解析器。
-
我已经添加了几次 bodyParser 但给我一个错误,也许我有一个错字,现在它可以工作了,谢谢