Reference Fiels - How to access by code

I get a OBJECT that contains a Reference Field LOJA:

{“_id”:“8eeef7fb-6804-4198-8dc5-590d909825ae”,“title”:“07fb2607-a76b-4f88-934b-24a658ca2974”,“email”:“xxxx@xxxx”,“_owner”:“07fb2607-a76b-4f88-934b-24a658ca2974”,“_createdDate”:“2017-09-22T17:05:30.754Z”,“_updatedDate”:“2017-12-23T19:14:54.315Z”,“idm”:“xxxxx”,“aprovado”:null,“loja”:{“ref”:“4dd1f032-e48b-4562-93e0-be37f67b9292”},“link-usuarios-_id”:“/usuarios/8eeef7fb-6804-4198-8dc5-590d909825ae”,“link-usuarios-all”:“/usuarios/”}

How to access de object LOJA, try loja.title but not work!
firstItem.idm = xxxxx
firstItem.loja = ref:“4dd1f032-e48b-4562-93e0-be37f67b9292”
firstItem.loja.title = ERROR

Same for INSERT, what should I put in the field?

I have the exact same problem for inserting data into a reference field. No way to populate it but by using the Collection Editor. I even tried creating an object and passing it to the field like that :

let newEntryRef = {
“ref” : “”
};
newEntryRef.ref = wixUsers.currentUser.id;
newEntry.member = newEntryRef;

I tried passing it as a String and no more luck.

I found a way to get it working.
define the new element’s reference as an object.
exemple for the member field below :

	// Définit les propriétés générales
	let newElem = {
	  "title": 		"",
	  "nom":   	$w("#inputNom").value,
	  "plf":		"",
	  "edt":		"",
	  "fmwk":		"",
	  "date":		$w('#datePick').value,
	  "member":	 	{ "ref" : "" }
	};
	
	// Définit l'identifiant unique :
	// calls a proc to generate a unique id
	newElem.title = getUniqueID();
	
	// Populate other fields...
	[...]

	// seems to be working when using a fixed value like that
	//newElem.member= {"ref":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"};
	
    	let userIdg = "";
	console.log('User ID : ' + wixUsers.currentUser.id);
	// Check that user creating this Element is a Member.
	wixData.query("Member")
      	.eq("_id", wixUsers.currentUser.id)
      	.find()
      	.then((results) => {
		if (results.items.length !== 0) {
		    	console.log('User is part of Member collection');
		        // Le user courant est un member. 
			// On ajoute la ref au record à insérer
		        userIdg = wixUsers.currentUser.id;
		}
		
		newElem.member.ref = userIdg;
		// Add the new element to the Element collection
		// with its member field linked to the Member collection
		wixData.insert("Elements", newElem)
			.then( (results) => {
				let item = results;
				console.log(item);
			})
			.catch( (err) => {
				let errorMsg = err;
				console.log(errorMsg);
			});

		});

Note : I was first trying to insert outside of the first “then” block, that’s was my value wasn’t populated at all…

Dear Bruno Couto Gomes ,

Did you ever found a simple solution for your problem?

I have a main table, a second and third table with a reference (one value) to the main table.

How can I access the second and third table items in a simple way?


Following code works fine…

$w(‘#dynamicDataset’).onReady(() => {
let displayItem = ;
displayItem = $w(‘#dynamicDataset’).getCurrentItem();
$w(“#text36”).text = toonAdres(displayItem.adresStraat, displayItem.adresPostcode, displayItem.adresPlaats);
$w(“#text34”).text = toonPrijs(displayItem.prijs);
});


but HOW TO access items in a dataset that references TO ‘#dynamicDataset’?

how to access the “field1”, “field2”, “field3” for the ‘dataset1’ or ‘dataset2’ items filtered to currentItem of ‘#dynamicDataset’?

Do I really need to reQuery?!

I see the data is loaded correctly in the different datasets (using the dataset links in the WIX Editor), I just need to access them via code.

This is how I did it… might be usefull to somebody else…

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import {toonAdres} from ‘public/myJavaScript.js’;
import {toonPrijs} from ‘public/myJavaScript.js’;

$w.onReady( function () {
$w(‘#dynamicDataset’).onReady(() => {
let vastgoedItem = $w(‘#dynamicDataset’).getCurrentItem();
$w(“#text36”).text = toonAdres(vastgoedItem.adresStraat, vastgoedItem.adresPostcode, vastgoedItem.adresPlaats);
$w(“#text34”).text = toonPrijs(vastgoedItem.prijs);
});

$w('#dataset1').onReady(() => { 
    $w('#dataset1').getItems(0,20) 
        .then( (result) => { 

let galleryItems = result.items;
for ( var i = 0; i < galleryItems.length; i++) {
var galleryItem = galleryItems[i];
console.log(“[” + i + “] " + galleryItem.title);
console.log(galleryItem.afbeelding);
if (!galleryItem.url){
console.log(“Geen URL”);
} else {console.log(”[url]: "+ galleryItem.url);}
}
} )
. catch ( (err) => {
let errMsg = err.message;
let errCode = err.code;
console.log(errCode + " : " + errMsg);
});
});
});