@w('#mydataset').OnReady doesn't seem to be working

I have the below code and I don’t understand the results I’m getting. There are two issues: I get one count using setInterval (correct count), another count if using dataset onReady, and no count using setInterval if I hit the browser refresh button. I created code to getTotalCount of a dataset and display counts in a text in the header just to see the counts. See below:

import wixUsers from 'wix-users';
import { session } from 'wix-storage-frontend';
import wixData from 'wix-data';

let user = wixUsers.currentUser;

$w.onReady(function () {

    // update header text based on interval
    setInterval(function () {
        $w('#textGroupSelectedName').text = session.getItem("groupAdminName");
        $w('#textCustomerCount1').text = $w("#dataset13").getTotalCount().toString();
        // above line initiallhy results in 2 which the the correct filtered count
       // however, if I hit the browser refresh button the resulting count is 0
    }, 1000);

    // when header user full dataset is ready (user login email text is updated)
    $w("#dataset21").onReady(() => {  // this is the user full dataset to get loged in email
        // header text
        $w('#textGroupEmail').text = session.getItem("groupEmail"); // get login email
        // $w('#textGroupEmail').text is connected to user full dataset in header

        // set customer profile dataset filter
        let userEmail = $w('#textHeaderEmail').text; 
        
        // save user login email to session variable
        session.setItem("groupEmail", userEmail);

        // filter dataset based on matching email
        $w("#dataset13").setFilter(wixData.filter()
            .eq('customerEmail', userEmail)
        );
        
        // show repeater and update header dataset count
        $w("#dataset13").onReady(() => { 
            $w('#listRepeater').show();
            $w('#textCustomerCount2').text = $w("#dataset13").getTotalCount().toString();
            // above line results in 5 which is total dataset population not the filtered count
        });
        
    }); 
      
});

Site is in development but is published. I’m using Wix Editor in desktop mode and the purpose is to store the signon user email in a session variable in order to use the stored value as a parameter in queries thoughout the site. My browser is Brave (Chromium based) and I’m running on Linux Manjaro.

Please help!

setInterval doesn’t consider when the dataset is actually ready so it won’t reliably give a count. All dataset calls should be inside the dataset’s onReady.

Since setFilter is a promise it might not be set by the time this code gets called:

To ensure that it is use either .then() or async/await to make sure setFilter is done before calling getTotalCount(): How to use promises - Learn web development | MDN

Thank you Anthony for your reply. It was helpful. For documentation purposes, here is how I solved the problem. I was using two datasets to complete a query. One dataset was connected to a members collection to retrieve the users email. The second data is what is connected to the repeater. I was trying to use a connected text field to the first dataset as a query parameter to the dataset for the repeater. The query didn’t work because it was running before the text field for user email was updated. So I took a different approach after doing more research. I hope this is the right way to do it.

In masterPage.js, I have code to do certain things based on user role. So, I added a session variable to this code. If a user is logged in, store the login email to the session variable. Then use the session variable in my queries throughout the site as needed. Here are some snippets:

import wixUsers from 'wix-users';
import { session } from 'wix-storage-frontend';

let user = wixUsers.currentUser;

    // if logged in
    if (user.loggedIn) { 

        // get logged in user email
        user.getEmail()
        .then ((email) =>{
            session.setItem("groupEmail", email);
        });
        ... continue on

This way, user email is available throughout the site as a session variable (groupEmail) which is what I needed. I tried to include this code in authentication.onLogin but couldn’t get it to work. That’s where it seems to belong since you only need to create the session variable once at login. The above solution creates it each time a page is loaded. But it works.

1 Like

Glad you got it figured out and thank you for taking the time to detail your solution. Much appreciated!