Repeater not populating on first page load (solved)

I just wanted to post here for the benefit of the community as I had quite the time finding a solution for this.
The problem was that the repeater on my page would just be showing the place holders and not the data I wanted on the first time a page was loaded. This was not evident in the Preview and only became obvious when the page was published. Even at this stage it was not always repeatable.

So, I finally found an answer to this:
The repeater code was originally put in an export function. This is where the fault was discovered. I needed to put the repeater loading code inside the OnReady function for the page. After doing this it has been working as expected. I also added a hide and show function for the repeater when I was testing and decided to leave it in there.

$w . onReady ( function () {

$w ( "#courseListing" ). hide (); 

wixData . query ( "Bookings/Services" ). eq ( "serviceType" ,  "CLASS" ). find () 
    . then (( results ) => { 
        **var**  data1  =  results . items 
        data1 . sort (( a ,  b ) => ( a . tagLine  >  b . tagLine ) ?  1  : - 1 ) 
        $w ( '#courseListing' ). data  =  data1; 
    }) 
$w ( '#courseListing' ). onItemReady (( $item ,  itemData ,  index ) => { 
    **const**  imageURL  =  itemData . imageURL ; 
    **const**  serviceName  =  itemData . serviceName ; 
    **const**  tagLine  =  itemData . tagLine ; 
    **const**  description  =  itemData . description ; 
    **const**  priceSummary  =  itemData . priceSummary ; 

    $item ( "#imageClass" ). src  =  itemData . imageURL ; 
    $item ( "#className" ). text  =  itemData . serviceName ; 
    $item ( "#tagLine" ). text  =  itemData . tagLine ; 
    $item ( "#description" ). text  =  itemData . description ; 
    $item ( "#price" ). text  =  itemData . priceSummary ; 
    $w ( '#courseListing' ). show (); 
}) 

});

This works.