Display data from different collections using velo

Hi there
I want to display on a repeater data from “collection1” with velo coding.
It works fine using this method [ link ]
Here I would like also to display data from another collection, called “collection2”. So I try to adapt the previous method.
In “collection1”, one field is the id of the good item of “collection2”. I want to display a specific field (called “theField”) of “collection2”
I naively tried

$item("#text").text = itemData.idCollection2._id.theField;

as “theField” was surprisingly proposed by completion…
I tried a more formal approach. In the onItemReady loop,

let idCollection2 = itemData.idCollection2 ;
wixData.query('Collection2')
        .eq("_id",idCollection2)
        .find()  
        .then( (results) => {
            console.log(results.items);
            let theFIeld = itemData.theField;
            } );
$item("#text").text = theFIeld;

But nothing happens…
Does anyone see the solution ?

When doing “manually” (without coding") [here] access to other collections is intuitive and easy. With velo, it seems to be much more complicated…
I hope to be wrong!

Thanks in advance,

Get referenced items
----------------------------------------------------
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.

import wixData from 'wix-data';

let myMainDB = "movies"; //--> Main DATABASE including RefField
let myMainID = "00001";
let myRefField = "actors";  //--> referencedField  

wixData.queryReferenced(myMainDB, myMainID, myRefField)
  .then((results) => {
    if(results.items.length > 0) {
      console.log(results.items[0]); //see item below
    } else {
      // handle case where no matching items found
    }
  })
  .catch((err) => {
    console.log(err);
  });

Hi

Thanks for your feedback. It helps me to go ahead.

A priori I don’t need to use the queryReferenced function as I just need to pick up one info from Collection 2. Thus it is just a reference field and not a multi-reference field (for non-experts: see here for the useful comparison).

Then a simple ‘include’ should be sufficient and here is my code:

wixData.query('idCollection1')
	.include('idCollection2')
	.find()
	...

(following the post here)
I guess it works fine as in the console there is a new subfield called ‘idCollection2’ in which I can see the expected info :

idCollection2: Array(1) json Table Copy JSON

0: {…} json Table Copy JSON

  theField: "NYIA"

However, when I try to display it in the repeater,

$item("#text").text = itemData.idCollection2.theField ;

I expected to see “NYIA” in the text box but nothing appears…

Any idea ?

The INCLUDE-METHOD (as i can remember) has a problem with the ID!!!

After you have used the include-method, does your data-package also INCLUDES → ID ???

Oh, as i can see, you even found the right post, for the INCLUDE-PROBLEM!
This is exactly the post, i was going to search for you :joy:

My suggestion → use the CONSOLE and inspect in detail the RESULT-OBJECT after you have used → INCLUDE

Using the structure of this example , my buildFiltersAndPopulateRepeater ( ) function is the following :

async function buildFiltersAndPopulateRepeater() {

    wixData.query("Collection1")
    .include('Collection2')
    .find()
    .then((selectedActivities) => {
        if (selectedActivities.totalCount > 0) {
            $w("#myRepeater").data = selectedActivities.items;
            console.log("Query results:", selectedActivities.items);
        } 
    })
    .catch((error) => {
        console.log("Error:", error.message);
    }); 
}

In this case, I can see the theField field appearing in the console as written earlier.

However, during the initRepeater() function, I also display on the console :

function initRepeater() {  
    
    $w("#myRepeater").onItemReady(($item, itemData, index) => {

        console.log("Query results 2:", itemData);
(...)
        $item("#text").text = itemData.idCollection2.theField;
    
    }); 
}

Here, when displayed on the console, no reference to Collection2 data!

In the first phase,

$w("#myRepeater").data = selectedActivities.items;

should states that all data are transfered to the #myRepeater

Thanks for your support

Hi
I fixed one issue: idCollection2 was typed “multi reference” and not only “reference”.
But still not recognising the Collection2…
What’s surprising me is that there is no reference to idCollection2 in the initRepeater function; only in the query…