Getting specific items in a Repeater with queryReferenced()

I have two databases in my website: A “Location” collection and a “Activities” collection. The Location collection contains overall information for guides (for example the name of the guide and the regions it´s about, just Text information ). The Activities collection contains information about certain locations with texts and images.

Each item within the Activities collection “belongs” to an item within the Location collection, so i thought about working with the queryReferenced() function. But there are some questions:

  1. The repeater i want to use to display the items from the Activities collection is within a Dynamic Page. That Dynamic Page is connected to the “Locations” dataset. So i need the repeater to change for each item from that collection respectively.

  2. I tried to work with the example code from https://www.wix.com/corvid/reference/wix-data.html#queryReferenced .
    The description says: This example finds the item in the Movies collection with the ID 00001 . It gets all of the items referenced in the Actors field of that item and then stores the first actor in a variable.

The code:

wixData.queryReferenced("movies", "00001", "actors")   .then( (results) => { if(results.items.length > 0) 
{let firstItem = results.items[0];}
    else {//handle case where no matching items found } } )
    .catch( (err) => { let errorMsg = err;});

The second reference in the first row (“00001”) is referring to a specfic item from the Movies collection. Is it possible to write something like currentItem? Because i want to use the code within the Dynamic page.

  1. Is there any reference to the other collection within that code? How would i refer to my Activities collection?

Greetings, Dawda

1 Like

There is this example here that might help you.
https://www.wix.com/corvid/forum/community-discussion/example-wix-data-multiple-references

Comes quite close to what i was looking for.
But after thinking a bit more about my issue, maybe working with the Reference field in my database might work better. I´ve put a Reference field into my Activities collection and connected the items to the respective item in the Locations collection.
I am not sure about the code (in fact i dont really know how to code in WIX).

Can i use something like this to show the right items in my repeater?
locationReference is the field in the Activties collection where the item from the location collection is referenced.

import wixData from 'wix-data';
$w.onReady(function () {

});
wixData.query("Activities")
.eq("locationReference", currentItem??)
.find()
.then((results) =>{
$w('#repeater1').data = results.items;
}, )

Thank you for your help so far! :slight_smile:

I think what you can do is set up another variable that gets the id of the current item, which is very simple, and then plug in that variable to the queryReferenced parameters.

let currentItem = $w('#myDataset').getCurrentItem();
let id = currentItem._id
wixData.queryReferenced("movies", id, "actors")   .then( (results) => { if(results.items.length > 0) 
{let firstItem = results.items[0];}
    else {//handle case where no matching items found } } )
    .catch( (err) => { let errorMsg = err;});

Hope this helps, and sorry if I got it wrong!