Collection to Repeater Lag

Hi everyone,

I am relatively new to coding with Velo and appreciate your assistance. I am struggling with populating a Repeater when the Collection changes.

I have a master list of Companies in one repeater that displays information on rows subject to filters. I have included a checkbox in each row in the master Repeater so that the user can select Companies of interest. I then load the selected companies into a Collection (I call tmpDbSelect for now).

I created another repeater that I called rptSelectedCompanies that should display only the companies selected by the user. This repeater is linked to the tmpDbSelect collection.

When the user checks or unchecks a checkbox, the tmpDbSelect correctly reflects the selected companies. However, the repeater rptSelectedCompanies seems to update the information erratically. Sometimes it ignores some a record though I can see it in the Content Manager table,

My reading of the forum so far has pointed me to the fact that the repeater rptSelectedCompanies is updating before the collection finishes its update. There were some suggestions of using async/await as a way to govern the order of code execution, but I have not been able to implement that solution successfully.

I would appreciate any insights and suggestions to help me resolve this issue. I am pasting the code snippet below.

Many thanks in advance.

export function iptSelectCompany_change ( event ) {
let activeUser = wixUsers . currentUser ;
let $item = $w . at ( event . context );
let myCompany = $item ( ‘#txtCompanyName’ ). text ;

**if** ( $item ( "#iptSelectCompany" ). checked ) { 
    wixData . insert ( "tmpDbSelect" , { 
        "userId" :  activeUser , 
        "selectedCompanies" :  myCompany 
    }); 
}  **else**  { 
    wixData . query ( "tmpDbSelect" ) 
        . eq ( "selectedCompanies" ,  myCompany ) 
        . find () 
        . then ( r  => { 
            wixData . remove ( "tmpDbSelect" ,  r . items [ 0 ]. _id ); 
        }) 
} 

populateSelectCoRepeater (); 

}

export function populateSelectCoRepeater ( ){
wixData . query ( “tmpDbSelect” )
. find ()
. then (( results ) => {
$w ( “#rptSelectedCompanies” ). data = results . items ;
})
}

Before calling populateSelectCoRepeater() , you need to handle the Promises of the other wixData functions and make sure that they resolve before continuing. See the following for information about Promises:

Thank you Yisrael.

In this case, the other wixData function that that is producing a Promise is the .insert function. Reading through the articles you kindly provided, it seems that a .then statement placed after the .insert function would ensure that the code waits for the wixData function to be resolved before executing populateSelectCoRepeater .

But I am out of my depth here because when I add the code: . then ( populateSelectCoRepeater ()) I get the error Argument of type ‘void’ is not assignable to parameter of type ‘(value: any) => any’.

I am not sure if I am not calling the populateSe… correctly or if I still don’t have a good grasp of structuring wixData functions (likely both).

I appreciate any guidance to get to the solution.

Amine

Amine, as you correctly state, you are not calling populateSelectCoRepeater correctly. Where you are currently calling it, the wixData functions are not yet completed. The results from wixData queries appears in the .then() functions only when the queries are completed. It is in the then() function that the results from the query will be available.

See the article Velo: Working with Promises which explains how to call wixData functions and correctly use the results. See the section on then() for an explanation.