Data is NOT ready on onReady()

Hy guys. I will be very quick:

  • My Dataset: 50 rows
  • My dataset’s onReady() on Page section:
    let x = $w(“#dataset1”).getTotalCount();
    console.log( x.toString(10) );
  • Console Result: 0!!!

WTF?!?!?!

It’s a little difficult to figure out what’s going on without a code snippet. I’m assuming the onReady you are referring to is the page’s on ready. If so, the dataset is in fact not ready yet at that point. Try getting the count inside the dataset’s onReady https://www.wix.com/code/reference/wix-dataset.Dataset.html#onReady.

Sorry Sam, i forgot to say that the code is inside Dataset’s onReady() for sure. :+1:t2:

Ok guys, I’m returning with a solution AND a problem that, I think, will be a challenge to WIX.

As showed above, I have a DataSet linked to my “dsOutdoor” collection with images and, each time the page refreshes, I want to change de Background Image of some ColumnStrip’s.

What did I do?
On DataSet’s onReady(), wrote a code to randomly get one record and then change the ColumnStrip’s Background image with selected row by setCurrentItemIndex().

Problem: as you guys can see on the images above, my .getTotalCount() alway returned 0! Wait, the Dataset is linked to a Collection, and in onReady this Dataset WAS NOT ready?!?

1 - First shot: added a .refresh() on first line of the .onReady(). Didn`t worked well. It looked like sometimes it changed the image but at most time don’t.

2 - Second shot: So I tryed to do a Query on PAGES’s onRead() this time, deleting all DataSets and its .onReady() functions. Hey! It WORKED!!! But not so well again; better than first shot, but not so well yet. I can say that, on the “First Shot”, 80% of time the code didn’t change the image. Now, in the “Second Shot”, it STILL DOESN’T change in about 40% of times.

So, I putted one line, for example, to clear the Background Image of the ColumnStrip and, just after, to change the IMAGE AGAIN with query results, just like this:

$w.query(“myCollection”)
1 .find()
2 .then( (results) => {
3 let count = results.totalCount;
4 let y = count * Math.random();
5 let pos = Math.round( y );
6 let item = results.items[ pos ];
7 $w(“#text1”).text = item[“title”];
8 $w(“#columnStrip1”).background.src = “”;
9 $w(“#columnStrip1”).background.src = item[ “c_foto” ]; //“c_foto” is for sure the reference field
10 )}
11 .catch( (err) => {
12 $w.alert(err);
13 )};

So… in the line 8 it really clears the ColumnStrip’s background, AND JUST THIS!!! THE NEXT LINE IS NOT EXECUTED!!! And, believe me, the line 7 prints the content of “title” field. And, believe, ALL rows have an image.

Finally, putting a query on my onReady() worked better than using a DataSet. But I’m intrigued why the code is not able to change the elements sometimes!

Please post the Editor URL of your site so that we can inspect.

hi there, how did you solve it?

i need to modify some dropdown options but it is not ready

$w.onReady too early
$w(“#myDataset”).onReady too early

$w(#dropdown1) does not have an own .onReady so how do i wait until its finished?

Please note that this is an old thread on a different topic…

A dropdown does not need an onReady() as all components are ready when the page is ready - that is, when the page’s onReady() event handler runs.

What isn’t working with your dropdown?

@yisrael-wix hi and thx for replying!

on this page i tried to fill the green #dropdown3 with data: (unsuccessfully)
https://www.angelika-lex.de/Buchtipps

import wixData from 'wix-data';         let dropdown_OPTIONS = [];
$w.onReady(function () {
 let filter_qry_not_lern=wixData.query("Buchkategorien").eq("waehlbar", true)
    wixData.query("Buchkategorien").not(filter_qry_not_lern).find().then( (results) => {
        results.items.forEach((item) => {
            dropdown_OPTIONS.push({"label" : item["anzeige"], "value": item["_id"]});
        })
    });
    $w("#dropdown3").options = dropdown_OPTIONS;    
});


But it is still empty. :frowning:

My dirty workaround (white #dropdown2) : Fill the dropdown when mouseOver event is triggered. But it cannot stay this way…

You need to set the dropdown’s options property inside of the .then(), like this:

let filter_qry_not_lern=wixData.query("Buchkategorien").eq("waehlbar", true)
    wixData.query("Buchkategorien").not(filter_qry_not_lern).find().then( (results) => {
        results.items.forEach((item) => {
            dropdown_OPTIONS.push({"label" : item["anzeige"], "value": item["_id"]});
        })
        $w("#dropdown3").options = dropdown_OPTIONS; // moved here
    }); 

this one works, thanks. Why was mine not working? Setting it later shouldn’t be a problem?