Table not loading properly

I have two collections. I pick out a subset of records from the first collection and based on an _id field, I need to look up that id in the second collection, get some data and display it in a table.

I use ‘async’ and ‘await’ to make sure the query calls are all completed. The result though doesn’t always display the same number of records in the table. It’s as if each time it runs, some records are skipped.

export async function loadTable ( )
{
await wixData . query ( “Collection1” )
. ge ( “dateTime” , new Date ())
. find ()
. then ( ( results ) =>
{
if ( results . items . length > 0 )
{
for ( let t = 0 ; t < results . items . length ; t ++)
{
loadData (( t == results . items . length - 1 ), results . items [ t ]);
}
}
} )
. catch ( ( err ) =>
{
console . log ( err );
} );

}

export async function loadData ( last , res )
{
await wixData . query ( “data” )
. eq ( “_id” , res [ ‘Id’ ])
. find ()
. then ( ( rs ) =>
{
if ( rs . items . length > 0 )
{
rows . push ({
“date” : res[‘dateTime’] . toDateString (),
“time” : (( res[‘dateTime’] . getHours () < 10 )? “0” + res[‘dateTime’] . getHours (): res[‘dateTime’] . getHours ()) + “:” + (( dat . getMinutes () < “10” )? “0” + res[‘dateTime’] . getMinutes (): res[‘dateTime’] . getMinutes ()),
“firstName” : rs . items [ 0 ][ ‘firstName’ ],
“lastName” : rs . items [ 0 ][ ‘lastName’ ]});
if ( last ) $w ( “#tblApp” ). rows = rows ;
}
} )
. catch ( ( err ) =>
{
let errorMsg = err ;
} );
}

Since you have a .then() function to handle the Promise returned by the query, the use of await is incorrect and will lead to errors and unpredictable results. You should delete await from both query statements.

Thanks Ysrael but after removing await and async, I’m still getting different results. One time it loads three records, another time it loads only one.

@internationalcovidsu Add a console.log(results.items.length) to see how many items were returned from the query. You are performing a query based on “today’s” date and time - Maybe your query isn’t consistent.

@yisrael-wix I did. It returns a variable number of items.

@internationalcovidsu Well, since you are filtering according to new Date() , every time you run it, the time will be different, and therefore the results could very well be different.

You’ll need to add some more console.log() statements to see what date is being filtered, and then check the database contents to see if the results make sense.

As an experiment, you should try a fixed date to use for the filter to see if the same results are returned each time.

@yisrael-wix Tried with fixed date. Same problem. I only have three records in the collection and it randomly picks one, two or all three.

@internationalcovidsu Which collection only has three records? Collection1?

Seems like I don’t see all of your code. Where is rows defined? You probably should push to the rows array in the loop in the loadTable() function, and then when the loop is finished set Table.rows.

What does the Collection1 query return if you leave out the filter? Does it change the number of returned items each time?

I just noticed that you have a typo in your code. The query of the data collection returns rs as the results, but your code uses res . As an example:

"date": res['dateTime'].toDateString(),

Seems like your code has other issues that need to be fixed.

@yisrael-wix Collection 1 has 4 records: 3 have dates past today’s date and one has a date which is prior to today’s. If I put a console.log statement in the Collection 1 query it always shows me 4 records if I remove the filter and 3 records if I put it back. So the issue lies with the second the ‘data’ collection query. “date” : res [ ‘dateTime’ ]. toDateString (), is correct because I’m getting the parameter from Collection 1. Silly me for using similar variable names, sorry, but it’s correct that way.

I solved it putting a small timeout in between loads of the single records. Now it always picks up the ones it should.

Excellent. Just finished lunch and thought about the same thing as I was finishing. Was going to let you but you figured it out. That’s great.

The other solution might be to use a Repeater, but if it works - don’t fix it.

@Ysrael On another note, I saw your great example of how to add Google Calendar events using Google API’s. Could you work one out for adding a calendar? I’ve been trying this example (https://developers.google.com/calendar/api/v3/reference/calendars/insert?apix=true#try-it) , but no go.