Passing multiple values to a button in repeater

Hi. Trying to assign the link of a button in repeater based on certain values from multiple referred databases.

My dataset for the repeater is set for MainProductCategories database which has 2 columns referred to 2 separate databases called teleskoptrucks and trucks.

$w.onReady(function () {

$w("#dataset1").onReady( () => {
$w("#repeater1").onItemReady( ($item, itemData, index) => {
  
 
  let teleskopref = itemData.teleskoptruckReference; 
  let truckref = itemData.truckReference;
  
//  let liftref = itemData.liftReference;

   console.log(teleskopref);
   console.log(truckref);
   //console.log(liftref);

     
if(itemData.mainCategory === "Teleskoptruck"){

               wixData.query("Teleskoptrucks")
                .eq("_id", teleskopref)
                .find()
                .then( (results) => {
                  let link = results.items[0]['link-teleskoptrucks-title'];
                  console.log(link);
                  $item('#button3').link = link;                              
                });

                if(itemData.mainCategory === "Truck"){

                wixData.query("Truck")
                .eq("_id", truckref)
                .find()
                .then( (results) => {
                  let link = results.items[0]['link-truck-kategori'];
                  console.log(link);
                  $item('#button3').link = link;
            
                });
                }
}

     else {
           $item("#button3").label = "No Value Given!";
      }

 console.log("The dataset is ready");

    } );
  } );

I get the ID value from let teleskopref = itemData.teleskoptruckReference; perfectly. But for let truckref = itemData.truckReference; I get a whole array of the referred database value instead of just the ID value!

Once I get the reference IDs I would like to query the different databases based on the value of the mainproduct category in the dataset.

Console log snapshot below:

How can I firstly get just the ID instead of the whole array.

And secondly is my IF function stated correctly to change the value of the button url to separate dynamic pages accordingly.

Please do let me know if there’s any assistance I can get. Would be massive. TIA.

for your first question, if you click the little arrow in the console next to your object, you will see what is available to you. it could be as simple as adding .id to the end like this itemData . truckReference.id but you will have to see what is in the object to know.

At a glance I think your button code is correct, but here is the documentation in case you have any trouble

Thanks for replying. I tried that too. but console log gives an error Cannot read properties of undefined (reading ‘_id’)

Just to clarify this is how I’ve setup the database.

console log for teleskop id shows correctly teleskop ref id44024912-81ea-4ae8-81d2-5ce78b96939e

console log for truck id shows as an array instead {…}

jsonTableCopy JSON
loftekapasitetKg: "3000 Kg"
model: "El 4-Hjul 3000 Kg"
_id: "ad2faac5-3e69-423b-bcc4-e43aed41405c"
owner: "044ce6b7-b149-4b10-a42c-d55e9a4e251d"
_createdDate: "Thu Jan 27 2022 20:18:48 GMT+0530 (India Standard Time)"
bilde:"wix:image://v1/044ce6_0f559642b5d04576ad4570c8f5e61dad~mv2.png/Diesel.png#originWidth=176&originHeight=132"
_updatedDate: "Sun Feb 06 2022 14:33:06 GMT+0530 (India Standard Time)"
kategori: "EL 4-Hjul"
bildegalleri: [...]
link-truck-all: "/truck/"
link-truck-kategori: "/truck/el-4-hjul-3000-kg"

THIS IS THE VALUES OF THE REFERRED DATABASE. WHICH IS WEIRD/UNIQUE? 

Why is that one referred column shows the proper id but the others show an array instead?

All I want is that the lettruckref=itemData.truckReference; gives out a proper id.

I am not sure why it is giving you an array, has to be something about how the multi reference is set up, but you should still be able to get to the id regardless within that returned object because

if itemData.truckReference is giving you the object, then:

itemData.truckReference._id should get you to the id.

but you are saying that gave you undefined? If you get any errors, or expand the json object again, can you screenshot the console?

You may need to take a look at some other forum posts on multireference data fields to get to the root of the issue. Here is also a YT video where a coworker of mine gets the id of a connected table and puts it in a column:

@amandam this has been solved.

the field wasn’t a multireference. only single reference.

a comment from a velo fb group made me think about just deleting the existing single reference field and making a whole new one to see if it works. and it did!

Highly annoying but atleast the problem is solved. Thanks again!