【发布时间】:2021-09-09 15:09:39
【问题描述】:
请谁能告诉我如何在此函数之外获取我从req.body 获取的数量变量或其数据?
app.post("/pay", (req, res) => {
console.log(req.body);
const { amount , description , name } = req.body; //this is that amount variable
const create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal",
},
redirect_urls: {
return_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel",
},
transactions: [
{
item_list: {
items: [
{
name: name,
sku: "001",
price: amount,
currency: "USD",
quantity: 1,
},
],
},
amount: {
currency: "USD",
total: amount,
},
description: description,
},
],
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for (let i = 0; i < payment.links.length; i++) {
if (payment.links[i].rel === "approval_url") {
res.redirect(payment.links[i].href);
}
}
}
});
});
app.get("/success", (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
payer_id: payerId,
transactions: [
{
amount: {
currency: "USD",
total: amount, // I want it here also
},
},
],
};
paypal.payment.execute(
paymentId,
execute_payment_json,
function (error, payment) {
if (error) {
console.log(error.response);
;
} else {
console.log(JSON.stringify(payment));
res.send("Success");
}
}
);
});
【问题讨论】:
-
我不太明白。这是一个看起来像快速服务器的请求回调。您还希望在哪里使用请求数据?显示实际调用
fetch的客户端函数(如果这是您要问的)。 -
@zero298 如果你能帮助我,我有评论我现在想要那个数量的可变数据
-
你能不能说明你想如何在这个函数之外使用它?我们不需要大量的细节,但我们确实需要minimal reproducible example。
-
我想使用 app.get("/success", (req, res)) 中的值
标签: javascript node.js express