Nested query on a dynamic page

Hi All
Hoping for some help with this problem… feels like I’m missing something obvious.

Essentially, I need to build a nested query on a dynamic page and output the results to a table on that same dynamic page.

I have two collections

  • teams
  • games
    Where games has two fields that both reference teams …
  • games.homeTeam is linked to teams.title and
  • games.awayTeam is linked to teams.title

fyi …this site is for a basketball tournament, where each team will want to know all the games they are playing. I have a dynamic page for each team (that displays the players on the team, their captain, ect) which is generated based on the teams.title

I’m stuck on how to build the query … specifically on how to define teams.title within the query…

wixData.query(“games”)
.contains(“homeTeam”, “???”)
.or(
wixData.query(“games”)
.contains(“awayTeam”, “???”)
)
.find()
.then((results) => {
console.log(results.items);
console.log(results.totalCount);

}) 
.catch((error) => { 
	let errorMsg = error.message; 
	let code = error.code; 
	console.log(errorMsg); 
	console.log(code); 
});

Hi,

if you are in a dynamic page, the page should contain a Dynamic Dataset component. You can access it with (if you haven’t changed the default name):

$w('#dynamicDataset')

It will have it’s current item set to the item the page is displaying (I assume it is the team page). So, you can replace ??? with

$w('#dynamicDataset').getCurrentItem().title

getCurrentItem() will return the current team being viewed.

I hope this helps. Let me know and have fun Wix Coding!

Thanks Giedrius!
Very helpful … I’m now uncertain on how to display the referenced fields. I have this code:

$w.onReady(function () {
$w(“#dynamicDataset”).onReady(() => {
let thisTeam = $w(“#dynamicDataset”).getCurrentItem();
wixData.query(“games”)
.contains(“team1”, thisTeam._id)
.or(
wixData.query(“games”)
.contains(“team2”, thisTeam._id)
)
.find()
.then((results) => {
$w(“#tableGames”).rows = results.items;
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
console.log(errorMsg);
console.log(code);
});

}); 

});

And get these results:

How do I replace the _id with the correct text from the referenced fields?

To replace IDs with actual objects, you should be using Reference fields (if you aren’t already). You can find additional information here:

If you have text fields with IDs now, you can just go and replace them with references. This will make it available in databinding, so you can actually bind fields from referenced items to the table (in your case team name and any other attributes of the team). There is an article explaining that here:

Now, you are using the API to populate the fields of the table. At this time, we don’t yet support the API way of including referenced object in the query (but we are working on making it available).

So, what you should do is instead of populating the table from data set via explicitly setting items from dataset to the table, is to bind the table directly to dataset and configure referenced item columns there.

So what you need to do is:

  1. Add second dataset for the games. I named it gamesDataset .
  2. Then add the following code to on ready handler of you dynamic dataset (note - you need to use eq for matching):
const thisTeam = $w("#dynamicDataset").getCurrentItem();
const filter = wixData.filter()
		.eq("team1", thisTeam._id)
		.or(
			wixData.query("games")
			.eq("team2", thisTeam._id)
		)
$w("#gamesDataset").setFilter(filter);
$w("#gamesDataset").refresh();
  1. Bind that dataset to games table, choosing the referenced items as detailed in How To Use Referenced Fields article.

Let me know how it went or if you need any further assistance.

Nice solution for binding the dataset to the table! When this is completely finished, I’ll write up the entire issue as it seems to be a popular question.

I do have a problem with the filter. As it is written now, I’m getting back all records in the games collection, regardless of the value of thisTeam._id

		const thisTeam = $w("#dynamicDataset").getCurrentItem();
		const thisFilter = wixData.filter()
			.eq("team1", thisTeam._id)
			.or(
				wixData.query("games")
				.eq("team2", thisTeam._id)
			);

		console.log(thisFilter);

		$w("#gamesDataset").setFilter(thisFilter);
		$w("#gamesDataset").refresh();

The console.log returns this:

{"filterTree":{"$and":[{"team1":"ddb20ff9-41c6-4b39-b4ed-997a622d6b01"}]},"invalidArguments":["Invalid .or parameter [Object]. .or expects FilterBuilder only."]}

Oh, indeed. My bad!

I missed this one:

wixData.query("games")

Needs to become

wixData.filter()

As we are combining filters.

Beautiful!