我正在创建一个筹款平台,但遇到了一些我认为是由 for 循环中的 Async/await 引起的问题。
从概念上讲,假设我有一系列人们已经承诺的捐款金额。当代码执行时,它将获取筹款目标并循环捐赠,对任何未收费的捐赠承诺收取费用,直到承诺用尽或达到筹款目标。然后代码更新我的数据库以显示使用了哪些新的捐赠承诺,或者有多少承诺可用,因此将来不会重新收费。此代码单独工作正常,但如果我尝试循环它,它会导致错误。
我认为问题在于循环的未来迭代在数据集保存并认识到已经使用了一些捐赠承诺之前开始执行。我认为我需要等待循环的未来迭代,直到保存先前的迭代?
有没有合适的方法来处理这个问题以及如何完成?
谢谢!
for (let i = 0; i < 2; i++) {
chargeDonations(proj_sub_id, projItem.price, applications.length, project_fees);
}
function chargeDonations(proj_sub_id, price, quantity, fees) {
wixData.query("Sponsorships")
.eq("proj_sub_id", proj_sub_id)
.ne("allFunds", true)
.find()
.then( (results) => {
let sponsorships = results.
let toCharge = (price + fees);
for (let i = 0; i < sponsorships.length; i++) {
let amount_charged = sponsorships[i].amountCharged;
amount_charged = amount_charged || 0;
let remaining_sponsorship = (sponsorships[i].amount - amount_charged);
let net_amount = (remaining_sponsorship - calculateFeesStripePhil(remaining_sponsorship, 1));
let net_toCharge = (toCharge + calculateFeesStripePhil(toCharge));
let sponsorships_sum = sponsorships.sum("amount");
if ((toCharge - net_amount) > 0) {
console.log("Donation Amount" + remaining_sponsorship);
console.log("Net Amount" + net_amount);
toCharge -= net_amount;
console.log("Still to be Charged: " + toCharge);
sponsorships_sum -= remaining_sponsorship;
$w("#dataset2").setFilter( wixData.filter()
.eq("_id", sponsorships[i]._id))
.then( () => {
$w("#dataset2").setFieldValue('amountCharged', (remaining_sponsorship + amount_charged));
$w("#dataset2").setFieldValue('allFundsUsed', true);
console.log("Saved Amount Charged");
$w("#dataset2").save();
});
}
else {
console.log("Sponsorship Amount " + (toCharge / .921 - 0.3).toFixed(2));
console.log("Net Amount: " + toCharge);
$w("#dataset2").setFilter( wixData.filter()
.eq("_id", sponsorships[i]._id))
.then( () => {
$w("#dataset2").setFieldValue('amountCharged', (toCharge + amount_charged));
console.log("Saved Amount Charged");
$w("#dataset2").save();
});
break;
}
}
});
}