onReady function doesn't run

Hey,
I want to count a dataset when the page loads.

I tried to make a onReady function, but it doesn’t work

$w.onReady(function () {
 let count = $w('#dataset1').getTotalCount();
 if (count > 0) {
        $w('#text111').text = `${count} Ziel`;
    }
});

But this function works:

export function repeater1_viewportEnter(event) {
 let count = $w('#dataset1').getTotalCount();
 if (count > 0) {
        $w('#text111').text = `${count} Ziel`;
    }
}

How can I run the code, as soon as the page loads ?

A dataset needs to load its data before you call its getTotalCount() function. Usually a dataset finishes loading a short time after the page it is on finishes loading. So if you call getTotalCount() inside the page’s onReady() event handler, the dataset might not be ready yet.
https://www.wix.com/corvid/reference/wix-dataset/dataset/gettotalcount

Try this →

$w.onReady(function () => {
  $w("#dataset1").onReady( () => {
   let count = $w('#dataset1').getTotalCount();
        if (count > 0) {
        $w('#text111').text = `${count} online`;
       }
  });
  
});

Oh yeah this works. I already tried the code, but I forgot the =>
I think thats why it didn’t work.

Thanks for your help :grinning:

Here’s the working code (you had one little mistake in it):

$w.onReady(function () {
    $w("#dataset1").onReady(() => {
 let count = $w('#dataset1').getTotalCount();
 if (count > 0) {
            $w('#text111').text = `${count} online`;
        }
    });