【发布时间】:2021-07-15 10:58:09
【问题描述】:
函数自行解析,函数不返回任何数据,但返回 null。 谁能帮我解决这个问题?
打印如下:
- [ RowDataPacket { payment_id: 'PAYID-MDRPLBY9LS10853614735924' } ]i>
- 数据不是 000000
- 数据不是 1111111
- promise2
- 获取付款响应
- 支付对象
但应该是:
- [ RowDataPacket { payment_id: 'PAYID-MDRPLBY9LS10853614735924' } ]i>
- 数据不是 000000
- 数据不是 1111111
- 获取付款响应
- 支付对象
- promise2
async function func(req, res) {
return new Promise((resolve, reject) => {
let email_id = req.body.email_id;
let arr = [];
let ctr = 0;
sql_get_orderid =
"Select payment_id from paypal.execute_order where email_id=?";
db.query(sql_get_orderid, [email_id]).then(function (data) {
console.log(data);
if (data.length != 0) {
console.log("data not 000000");
data.forEach((x) => {
console.log("data not 1111111");
var paymentId = x.payment_id;
paypal.payment.get(paymentId, function (error, payment) {
if (error) {
console.log(error);
reject({ auth: true, message: "Error on the Server" });
} else {
console.log("Get Payment Response");
console.log(JSON.stringify(payment));
arr[ctr] = payment;
ctr = ctr + 1;
// resolve(payment);
// if(ctr==data.length)
}
});
});
resolve(arr);
}
});
});
}
router.post(
"/get_all_orders",
VerifyToken.Token_Verification,
async (req, res) => {
let arr = await func(req, res);
console.log("promise2");
res.send({ data: arr });
}
);
【问题讨论】:
-
“自行解决”是什么意思?
-
承诺不是这样运作的。如果它得到解决,那是因为
resolve被调用了。如果用null解决,那么payment就是null。 -
@rayhatfield 如果 resolve 被调用,那么它应该返回支付对象,但它没有。我能够打印支付对象.....支付值是之后得到的,但函数提前返回 null。
-
您如何使用
getPaymentDetails?你所说的“提前返回”让我觉得你正试图从异步回调中返回数据。此外,它不需要async关键字,因为您从未使用过 await。 -
@KevinB 编辑了...的代码,还提供了付款ID供您查看
标签: node.js asynchronous async-await