Transport calculation and repeater results

I create a transport quote form based on:

  1. data entered into the form: Miles and Hours
  2. driver rate data from the database: rate per mile and rate per hour

I have many drivers in the database and each has individual rates for miles and hours.

I created a simple form with miles and hours to enter and a button after clicking which a repeater appears. The repeater should display the driver’s name (this works) and TOTAL Cost which does not work and gives the answer “£undefined”.
Total Cost should be calculated on the basis of:

(miles * rate per mile) + (hours * rate per hour)
where “miles and hours” are those entered in the form
where “rate per mile and rate per hour” are data downloaded from the driver database.

The repeater should display a list of all drivers with an individually calculated Total Cost for each.

I also want to add filtering by region to the form (data from the driver database)
and a button to pay for the selected offer in the repeater.

Please help because I have a deadline of Friday and I have been struggling with this for a few days. Even chatGPT can’t handle it.

I am attaching photos and used code below:

import wixData from “wix-data”;

$w.onReady(() => {
// Ukryj repeater na początku
$w(“#resultsRepeater”).collapse();

// Obsługa kliknięcia przycisku
$w("#calculateButton").onClick(async () => {
    const miles = Number($w("#milesInput").value);
    const hours = Number($w("#hoursInput").value);
	console.log('Miles:', miles);

console.log(‘Hours:’, hours);

    // Sprawdź, czy pola są wypełnione
    if (!miles || !hours) {
        $w("#errorText").text = "Wypełnij oba pola!";

return;

    }

    try {
        // Pobierz kierowców z bazy danych
        const results = await wixData.query("Drivers").find();

        // Oblicz koszt całkowity dla każdego kierowcy
        const drivers = results.items.map(driver => {
const totalCost = (miles * driver.ratePerMile) + (hours * driver.ratePerHour);
return {
    ...driver,
    totalCost: totalCost.toFixed(2) // Zaokrąglenie do 2 miejsc po przecinku
};

});

        // Powiąż dane z repeaterem
        $w("#resultsRepeater").data = drivers;

        // Pokaż repeater
        $w("#resultsRepeater").expand();
    } catch (error) {
        console.error("Błąd podczas pobierania danych:", error);
    }
});

// Konfiguracja repeatera
$w("#resultsRepeater").onItemReady(($item, itemData) => {
$item("#driverName").text = itemData.driverName;
$item("#totalCost").text = `£${itemData.totalCost}`;

});

});

Please format your code
```js
code
```

As for what I can figure out from what’s currently formatted in your message:

  • It seems you’re fetching the drivers list every time the user presses the calculate button - But the data for those drivers is always the same

And as for the Undefined you’re getting, try to log the values before calculating totalCost, perhaps one of them is undefined or of the wrong type?