Help with Messy Coding for Looping thru Repeater & Asyncing

Hey guys,

I have a DB collection showing Exchange Rates like below

And then,
I have a Repeater displaying info from another DB Collection like below

If you see the screenshot above I have a Currency Dropdown which retrieves and shows the Currency Exchange Rates from the ExchangeRates DB Collection as below


Now,
after the exchange rate is displayed I am calculating the amount to be displayed in the repeater. The initial amount is being held in the same DB the repeater info is coming from.

I am using the following code and its working fine :slight_smile:

export function dropdown1_change(event) {
$w("#repeater1").forEachItem( ($w) => {
        wixData.query("ExchangeRates")
                .eq('title', $w("#dropdown1").value)
                .find()
                .then((results) => {
 let Item = results.items[0];
                    getRates(Item);
                });
        });
}

async function getRates(Item) {
        $w("#exrate").text = Item.exchangeRates;
        $w("#currency").text = Item.title;
 await $w("#exbox").show();
 await $w("#repeater1").forEachItem( ($w) => {
        wixData.query("SalesInventory")
                .eq('title', $w("#iid").text)
                .find()
                .then((results) => {
 let item = results.items[0];
                    getFinalRates(item);
        });
    });
}

async function getFinalRates(item) {
    $w("#repeater1").forEachItem( ($w, itemData, index) => {
 let adultprice =  (Number($w('#exrate').text)) * (Number(itemData.adultPrice));
    $w('#aprix').text = String(adultprice);
    });
}

I just want to know from the pros if my method is clean or am I getting results but in a messy way ??

Any comments would be helpful

Hi Shan,

There is room for improvement here.

For every repeater item you have, you are calling wixData query twice (once in the first and once in the second function), while the results you receive in each iteration are the same.
For a more efficient flow, call wixData query once for each collection using the hasSome filter, which allows you to provide an array of strings to match against instead of querying one by one, then call getRates which will do the required conversion and finally getFinalRates to populate the repeater fields.

Is your repeater connected to a dataset?
Note that manually setting a repeater’s data (see here ) is probably preferred in your situation. Although it’s a bit more complicated to implement, it will allow greater flexibility in the future and better performance overall.

Ido

1 Like