1

我正在创建一个筹款平台,但遇到了一些我认为是由 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;
                }
                }

        });

}

4

1 回答 1

0

是的-您的假设是正确的,即在前一个循环迭代完成之前调用了下一个循环迭代。使用async/await或者Promise.all(/* array of promises*/)应该可以解决这个问题。

我相信正确的方法是这样做:

for (let i = 0; i < 2; i++) {
    await chargeDonations(...);
}

async function chargeDonations(...) {
    ...
}

此外,我将使用WixCode API检查 API中的哪些函数dataset(例如savesetFilter...)和$wAPI 返回 a并用块Promise包装它。async/await

这是我试图让您的代码更易于阅读和理解的内容:

for (let i = 0; i < 2; i++) {
  await chargeDonations(...);
}

async function filterBySubId(filter){
  /*...*/
}
async function filterBySponsorshipId(filter){
  /*...*/
}

async function chargeDonations(proj_sub_id, price, quantity, fees) {
    const results = await filterBySubId();

    let sponsorships = results;
    let toCharge = (price + fees);

    for (let i = 0; i < sponsorships.length; i++) {
      /* I would also put this block in a function and return values */
      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");
      /* end of calculations block */

      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;

        await filterBySponsorshipId(/* your filter */)

        $w("#dataset2").setFieldValue('amountCharged', (remaining_sponsorship + amount_charged));
        $w("#dataset2").setFieldValue('allFundsUsed', true);
        console.log("Saved Amount Charged");
        await $w("#dataset2").save();

      } else {
        console.log("Sponsorship Amount " + (toCharge / .921 - 0.3).toFixed(2));
        console.log("Net Amount: " + toCharge);

        await filterBySponsorshipId(/* your filter */)
        $w("#dataset2").setFieldValue('amountCharged', (toCharge + amount_charged));
        console.log("Saved Amount Charged");
        await $w("#dataset2").save();
        break;
      }
    }
}

希望它对您有意义并帮助您,干杯!

于 2018-02-07T09:34:40.470 回答