Collection Query Problem

Hello Everyone,

I am not a developer and do not know much and hence the reason I started with Wix back then. Having said that, I am so stoked about being able to add databases that I am giving it a try, but now I am stuck and hope that someone can assist me.

I have got a few collections for displaying TV Series Connecting my TVShows collection to a dataset on the Home page and displaying the TV Shows in a repeater, you can click the ‘View’ button on the TV show that will take you to a dynamic page according to title.

On the dynamic page I have another dataset connected to the TVShows collection with two text boxes:
#text52 = The number of seasons
#text63 = The TV show ID (ShowID in TVShows collection)

I want to do a query on my TVSeasons collection to count the the number of times the TV Show ID (ShowID) is showing.

Below see my collections and query:

TVShows:
ShowID(Primary key)>Title>Image>Description>TVSeason (Reference Field to TVSeasons - multi)

Field Key:
ShowID = showID
Title = title
Description = description
Seasons = seasons
TVSeason = tvSeason

TVSeasons:
Title(Primary key)>TVShows-2 (Reference TVShows)>SeasonNumber>Episode(Reference field to Episodes- multi)

Field Key:
Title=title
TVShows-2 = TVShows-2
Episode = tvepisode

EpisodesTitle:
Title(Primary key)>TVSeasons (Reference from TVSeasons)>Season>EpisodeNumber>EpisodeTitle>Description

Field Key:
Title = title
TVSeasons = TVSeasons
season = season_title
EpisodeNumber = episodeNumber
EpisodeTitle = episode_title
Description = description_episode

My query:
import wixData from ‘wix-data’;
// …
$w.onReady(function () {
//TODO: write your page related code here…
let value1 = $w(“#text63”).text;
wixData.query(“TVSeasons”)
.contains(“title”,value1)
.count()
.then( (num) => {
let numberOfItems = num;
$w(“#text52”).text = num.toLocaleString()
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );

});

If I replace .contains(“title”, value1) with .contains(“title”, “ShowID”) - it counts correctly but setting value1 to #text63 and use that in the query (remembering that #text63 is the ShowID displayed in a text box on-page), it returns 0.

.contains() according to the Wix code help can also make use of a reference - I have tried every property and reference field I can think of but nothing)

I then tried to make use of .include() But .include(TVShows-2) or any other include I could not get it to work to use the ShowID in the query

Please help this noob!

I found the problem for the code mentioned above in any case. The code runs before the text field is populated from the data-set. So you make sure the data-set has finished loading first before running the query. Like this:
$w.onReady( () => {
$w(“#myDataset”).onReady( () => {

I would still appreciated an example of doing a query on a referenced field in a collection.

Regards,
JD

Hi,

references fields hold ids of referenced items. The id of an item is always the _id field (it is not the same as the primary field! Primary field is used only for displaying the item in UI).

The query also depends, on whether you use single or multi reference. As I see, you use multi, you can query:

wixData.query('TVSeasons')
  .hasSome('tv-shows-2', tvShow._id)
  .find()

This will find all TVSeasons that are a part of the given show. If you have shows you can get tvShow by doing $w(‘#dataset1’).getCurrentItem().

If say, you modelled using a single references (tv-shows-2 was a singular reference to TVShows collection), you would use equals:

wixData.query('TVSeasons')
  .eq('tv-shows-2', tvShow._id)
  .find()

I hope this helps. Let me know!

1 Like

Thank you so much for the feedback. I will give it a try.

I have tested the code and it works a treat! Thank you.

No problem. Glad it helped!