Wix code SDK error: The "onItemReady" function of "rptReport" threw the following error: Cannot read property 'toString' of undefined

I have two radio buttons, two datepickers, a button and a repeater on my page and the following code that will fill the repeater. Everything works fine in when hit the button after selecting the start and end dates right after page load in Preview mode. However, when I change the selection in radio button I get many lines of errors and it give reference to a particular line of code and which is,

$w("#rptReport").data = results.items;

Note: it runs without errors on first choice radio button, no matter which option I have chosen. On changing the selection, I get the error. Yet, I get the result in repeater.

I try adding the following line of code in button click to see if this will fix the error, but I didn’t.

$w('#rptReport').data = []

Following is the code for button click

export function button7_click() {
    $w('#rptReport').data = []

 if ($w("#rdoReportFor").value==="Buy Orders") {
        findBuyOrdersBtwDates()
    }else if ($w("#rdoReportFor").value==="Sell Orders") {
        findSellOrdersBtwDates()        
    }else{
        console.log("Both cannot be displayed at the moment, to be coded")
    }
}

Following is the complete code of one of the fuctions findBuyOrdersBtwDates().
Code for the function findSellOrdersBtwDates() is not included in the question as both these functions are same except some field names in the database are different.

I do not know if there is a better way to do the same, please tell me the correct way to do it as I am a beginner.

function findBuyOrdersBtwDates() {
 
 let startDate = $w('#startDate').value;
 let endDate = $w('#endDate').value;
 let yearValue = startDate.getFullYear();
 let monthValue = startDate.getMonth();
 let dayValue = startDate.getDate();
 let startDateSrch = new Date(yearValue, monthValue, dayValue, 0, 0, 0);

    yearValue = endDate.getFullYear();
    monthValue = endDate.getMonth();
    dayValue = endDate.getDate();
 let endDateSrch = new Date(yearValue, monthValue, dayValue, 23, 59, 59);

    wixData.query("database")
        .between('bDate', startDateSrch, endDateSrch)
        .find()
        .then((results) => {
 if (results.items.length > 0) {

 let compName = new Array()
 let investment = new Array()
 for (let i = 0; i < results.length; i++) {

                    compName[i] = results.items[i].companyName;
                    investment[i] = results.items[i].bNetPrice;
                }
 let data = [
                    [investment],
                    [compName],
                ];

                $w('#html1').postMessage(data);
                console.log(data)
                $w('#html1').onMessage(async (event) => {

 if (event.data.type === 'ready') {
                        $w('#html1').postMessage(data);
                    }

                });

                $w("#rptReport").data = results.items;

            } else {
                $w("#message").show()
                console.log("item not found between these dates")
            }
        })
        .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;
        });

    setTimeout(() => {
        $w("#rptReport").onItemReady(($item, itemData, index) => {
            $item("#txtBuyDate").text = itemData.bDate.toLocaleDateString();
            $item("#txtCompName").text = itemData.companyName.toString();
            $item("#txtQty").text = itemData.bQuantity.toString();
            $item("#txtUnitPrice").text = itemData.bUnitPrice.toString();
            $item("#txtInvestment").text = itemData.bOrderCost.toString();
            $w("#columnStrip1").show()
            $w("#rptReport").show()

        });
    }, 500);
}

I hope my question is clear

Thanks in advance.

The correct location for the Repeater’s onItemReady() is in the page’s onReady() function, and not in the findBuyOrdersBtwDates which is called from button7_click() . The onItemReady() function is an event handler and is automatically called when Repeater items are ready - it is not meant to be called by your code.

See the onItemReady() API for more details.

Perfect.

Thanks a lot. It did fixed the issue.

Now I have created one more repeater, earlier I was using one repeater for both functions, and added onItemReady() of both repeater in onReady() as follows. Also, I removed setTimeout() which you can see in the last part of the code in my question.
Is the setTimeout() necessary?
Am I going right?
Any suggestion to improve my code?
It takes a bit of time to update the repeater whenever I change radiobutton selection and hit button.

$w.onReady(() => {

 
        $w("#rptReport").onItemReady(($item, itemData, index) => {
            $item("#txtBuyDate").text = itemData.bDate.toLocaleDateString();
            $item("#txtCompName").text = itemData.companyName.toString();
            $item("#txtQty").text = itemData.bQuantity.toString();
            $item("#txtUnitPrice").text = itemData.bUnitPrice.toString();
            $item("#txtInvestment").text = itemData.bOrderCost.toString();
            $w("#columnStrip1").show()
            $w("#rptReport").show()

        });
 


        $w("#rptReport2").onItemReady(($item, itemData, index) => {
            $item("#txtSellDate").text = itemData.sDate.toLocaleDateString();
            $item("#txtSellCompName").text = itemData.companyName.toString();
            $item("#txtSellQty").text = itemData.sQuantity.toString();
            $item("#txtSellUnitPrice").text = itemData.sGrossUnitPrice.toString();
            $item("#txtSellCost").text = itemData.sNetOrderPrice.toString();
            $w("#columnStrip1").show()
            $w("#rptReport2").show()
 
        });


});