Accessing referenced item using code

Hi, Iv’e been trying to access a property of a referenced item using code.
I have one data-collection called ‘A’, and one of it’s properties is a reference to an item from data-collection B.

On one of my pages I have a dataset connected to data-collection A.
In the code of the said page Iv’e been trying to access a property of the referenced item. Unfortunately, no matter what I have done - it didn’t work.

Example of the code:
var item = $(“#dataset1”).getCurrentItem();
var refItem = item.refItemB;
var number = refItem.number;

I’m trying to reach and access the “number” property .

Please help me to figure out this issue, Thank you very much!

Hi,
Check out with reference .

wixData.query("myCollection")
  .include("referenceField")
  .find()
  .then( (results) => {
    let items = results.items;
    let firstItem = items[0];
    let firstRefProp = firstItem.referenceField.propertyName;
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

Roi.

Well your solution does give me an access to the referenced properties. However, when I’m trying to implement this method inside a “forEachItem()” function of a repeater, I can’t define the repeated elements. It sets all the repeated items to be the same instead of having unique values.

Please help me, thank you very much!

Check out onItemReady method.
Roi.