Code only works sometimes

The code below works, kind of. Sometimes I have to refresh the page for it to work and I’m not sure why. Sometimes I have to refresh a couple times. To get to the page people click on a link with a query like so https://www.mytutorialcenter.com/training-progress?search=littletonma.myseniorcenter.net . I marked the 3 parts that I’m having issues with below. I thought maybe I needed

$w.onReady( function () {
$w( ‘#centers’ ).onReady(() => {
$w( ‘#progress’ ).onReady(() => {
$w( ‘#callLogs’ ).onReady(() => {
$w( ‘#notes’ ).onReady(() => {
let query = wixLocation.query.search;
console.log(query);

                wixData.query( 'Centers' ).contains( 'subdomain' , query) 
                    .or(wixData.query( 'Centers' ).contains( 'title' , query)) 
                    .or(wixData.query( 'Centers' ).contains( 'center_address' , query)) 
                    .limit( 10 ) 
                    .find() 
                    .then(res => { 

if (res.items.length === 1 ) {
const MyCID = res.items[ 0 ]._id
local.setItem( “CID” , MyCID);
$w( ‘#label’ ).text = MyCID
} else {
local.setItem( “CID” , ‘0000’ );
$w( ‘#label’ ).text = ‘Click Expand For the Center of Your Choice’
}

                    }); 

                wixData.query( 'Centers' ).contains( 'subdomain' , query) 
                    .limit( 10 ) 
                    .find() 
                    .then(res => { 
                        $w( '#centersRepeater' ).data = res.items; 
                        $w( '#centersRepeater' ).show(); 
                        $w( '#centersRepeater' ).expand(); 
                        $w( "#searchBar" ).value = query 

if ($w( ‘#label’ ).text === ‘Click Expand For the Center of Your Choice’ ) {} else {
const MyCID = $w( ‘#label’ ).text
wixData.query( ‘training_progrss’ )
.contains( ‘center_id’ , MyCID)
.limit( 100 )
.find()
.then(res2 => {
//these are the parts that sometimes don;t show unless I refresh the page
if (res2.items.length > 0 ) {
$w( ‘#trainingRepeater’ ).data = res2.items;
$w( ‘#trainingRepeater’ ).show();
$w( ‘#TrainingNotes’ ).show();
$w( ‘#trainingRepeater’ ).expand();
$w( ‘#TrainingNotes’ ).expand();
}
});

                            wixData.query( 'call_logs' ) 
                                .contains( 'center_id' , MyCID) 
                                .limit( 100 ) 
                                .find() 
                                .then(res3 => { 

//these are the parts that sometimes don;t show unless I refresh the page
if (res3.items.length > 0 ) {
$w( ‘#callRepeater’ ).data = res3.items;
$w( ‘#callRepeater’ ).show();
$w( ‘#CallNotes’ ).show();
$w( ‘#callRepeater’ ).expand();
$w( ‘#CallNotes’ ).expand();
}
});

                            wixData.query( 'notes' ) 
                                .contains( 'centers_id' , MyCID) 
                                .limit( 100 ) 
                                .find() 
                                .then(res4 => { 

//these are the parts that sometimes don;t show unless I refresh the page
if (res4.items.length > 0 ) {
$w( ‘#noteRepeater’ ).data = res4.items;
$w( ‘#noteRepeater’ ).show();
$w( ‘#gNotes’ ).show();
$w( ‘#noteRepeater’ ).expand();
$w( ‘#gNotes’ ).expand();
}
});
}

                    }); 

            }); 
        }); 
    }); 
}); 

});

Hi there …

From your code, I guess that you’re using datasets, and using data query, but why!? You should either use datasets + filtering, or data query, since you have multiple queries, I suggest that you use a backend module to get all the results, then return the final result to the frontend, this will increase performance significently.

Here’s an example

// backend module: contacts.jsw;
import wixData from 'wix-data';

export async function getDetails(phrase) {
    try {
        let response = {
            centers: await queryCenters(phrase),
            logs: await queryLogs(phrase),
            notes: await queryNotes(phrase)
        }
        
        return response;
    } catch(err) {
        return err;
    }
}
// Your page
import {getDetails} from 'backend/contacts.jsw';
import { query } from 'wix-location';

$w.onReady(async () => {
    if (query && query.search) {
        await getDetails(query.search).then((response) => {
            /*
            response = {
                centers: // Array,
                logs: // Array,
                notes: // Array
            }
            */
            
            // Apply the items as repeaters' data
        })
    }
})

Please note: I don’t see the onItemReady( ) function anywhere in your code, you need to define it before applying the items as a value of the repeater’s data.

Hope this helps~!
Ahmad

Thanks guys. I’ve changed things a bit and it works great now. I was definately over complicating things.

Glad that it worked :wink: