Why does this Multi Reference field return "undefined"?

My dynamic page has 2 datasets connected to it, “Wineries” and “Wines”. The “Wineries” collection has a Multi-Reference field named “wines” and I tried to get it using to getCurrentItem() as follows:

$w.onReady(function () {
  $w('#winesDataset').onReady(() => {
    console.log("wines data ready");
    $w('#wineriesDataset').onReady(() => {
      let wines = $w('#wineriesDataset').getCurrentItem().wines;
      console.log(wines);
    })
  })
});

And this is what I got from console:

wines data ready
undefined

Also Iv’e tried to switch between the onReady functions, and also to omit the one of the wines dataset, but all results were the same. How can I get the data from this field?

Hi Jonathan,

To get at the data for a multi-reference field, you will have to query it, something like this:

import wixData from 'wix-data';
$w.onReady(function () {
  $w('#winesDataset').onReady(() => {
     console.log("wines data ready");
     $w('#wineriesDataset').onReady(() => {
     // get the _id value of the the current item
     let Id = $w('#wineriesDataset').getCurrentItem()._id;
     // presuming name of multi-reference field 
     wixData.queryReferenced("wineriesDataset",Id, "wines")
      .then((results) => {
          console.log("Array of wines follows...");
          console.log(results.result.items);
      })
    })
  })
});