I have been working satisfactorily with collections linked by reference fields for some time but have now hit a problem.
I have a dataset with reference fields on a page. Using code I wait until the dataset is ready then get the items and send them to the console. Previously this has shown the collection items AND all of the referenced items as objects. I believe this is correct behaviour. Now the same code on a similar collection shows the primary item objects but the reference fields only hold an id rather than the referenced objects. On another occasion it has shown the objects for one of the referenced collections but not another two.
I feel that I am missing something somewhere and would appreciate some guidance to get this working correctly.
Can you please send a picture of your code, and the database?
Moreover, pay attention that your’e using include() function inside the Query() in order to get the full referenced item.
$w.onReady( function () {
//TODO: write your page related code here…
// check the List dataset is ready and get items
$w(“#dataset1”).onReady(() => { let count = $w(“#dataset1”).getTotalCount();
This same code used for different collections with reference fields yields different results as mentioned in my first post. I have never used include() in any of my code. I’ll try to show my collections later but I will need to sanitize them as they contain sensitive data.
I have just discovered the reason why I’m getting different results, but it doesn’t make a lot of sense to me!
In the first instance where I had the full reference objects shown I had previously used a table connected to the dataset on the page . When this table proved unsuitable for my purposes I deleted it and moved to using code. I could still access all referenced objects easily in code.
In the second instance I simply added a dataset to the page but no table. Using the same code the references are only shown as ids.
I have done some tests and shown this to be reproducible.
Creating a table to display the referenced data seems to trigger the dataset to include the references even after the table is deleted from the page. Rather odd behaviour, but I now have a long-winded way of achieving my requirements. I’ll have a look to see if include() can provide a more robust solution.
Using a data query with include() does indeed give me the data I require without having a dataset on the page at all. Thank you for that pointer.
The only fly in the ointment is that include() requires a list of reference fields. I have over 20 in my collection so quite a bit of hard-coded values. If I add an extra reference field to the collection I have to mirror that in the code. It would be nice if include() could have a wildcard so it includes ALL reference fields from a collection.
function reference() {
let referenceData
let referencedId = $w('#myDataset').getCurrentItem().referenceField
wixData.query("myCollection")
.eq("_id", referencedId)
.find()
.then((results) => {
if (results.items.length > 0) {
referenceData = results.items
} else {
console.log("No data was found")
}
}).catch((err) => {
console.error("There was an error:", err)
})
//use your data
$w('#textElement').text = referenceData.firstName
}