Hi,
I have few data collections.
in dynamic page of collection X id like to get some items from data collection Y.
I tried few codes from the reference but its i no go.
Please help!
For example:
X is the dynamic page collection.
Y is the other collection I’d like to get the data from.
this one works well
let currentItem = $w(“#X”).getCurrentItem(); //works well
not working since i get y = 0.
let y = $w(“#Y”).getTotalCount();
Hey
I believe that you need to wrap both of those lines inside an onReady for each dataset so that is fully loaded before using it.
Read more:
@zeev What do you get in the developer console when console.log(currentItem) and also console.log($w(" #Y ").getTotalCount())
@andreas-kviby Ok, now I got it, your comment helped, Thank you so much!
$w("#Y").onReady( () => {
let y = $w("#Y").getTotalCount();
} );
Now it got me into some new problem, im trying to get the values of a multi items referenced field inside this Y collection, any ideas ?
@Zeev D Have you read these articles:
-
CMS: About Reference Fields in Database Collections | Help Center | Wix.com
-
Adding a Reference Field to a Database Collection | Help Center | Wix.com
Understanding these is pretty important to helping you solve your problem.
If you read these you will find that the reference field can contain a single reference OR it can contain a collection of references.
Based on your question you are using the reference field capture a collection of references. Which requires the use of a special API call which we will get to.
OK So every record (item) that is stored in a data collection has a number of properties that are not normally visible in the Data Collection view. These are:
-
_id
-
_createdDate
-
_updatedDate
-
_owner
These are normally hidden when you view the data collection but you can add them to the view using the Visible Fields drop down:
When you look at the data collection without examining the property details you see the Field Name which is intended to be user friendly. However in code you need to use the Field Key value. You get to this by examining the field details like so.
Here you can see that the field named ID has a key value of _id .
OK this is important because it is THE ONLY field that guarantees that you have a unique record from the data collection. Every other field could be duplicated but this one cannot be. This is the REAL reference that is used in the reference field of any data collection.
When you assign an item object to a reference field the Wix Code API figures out that you are trying to create a reference and adds the _id for you from the item.
So how to use these reference fields?
Well let’s say, using your example, you have two datasets $w(‘#X’) and $w(‘#Y’). In data set $w(‘#X’) you have a reference field that can contain one or more references to items in dataset $w(‘#Y’). Which you have configured in your data collection as shown here ----------->
How do you add references and how do you access references? Well first of all you need to understand that by checking the Multiple Items switch what you are essentially doing is creating what is called a Many-to-Many binding. This looks like an array of references in the data collection you created the reference field in. At the same time another reference field is created in the data collection that you have referred to which contains the reverse references.
Now because you have to search and retrieve reference arrays you need to use a special set of wix-data APIs and they are:
I will let you read the linked API references for these functions but here is an example of one to get you going:
InsertReference
Syntax
function insertReference(collectionName: string,
propertyName: string,
referringItem: Object | string,
referencedItem: Object | string | Array<Object> | Array<string>,
options: WixDataOptions): Promise<void>
This function call tells us that we need to identify the collectionName for the collection we are inserting our reference into, the property (or field) name of the reference we are inserting information into, the row in the collection we want to update (this is the unique ‘_id’ for that row, and finally what we are inserting into the data collection.
Let’s break this down:
referringItem: Object | string
This means that the referringItem argument (also known as a parameter) can be the string value of the ‘_id’ OR you can simply pass the item (also known in javascript as an Object) for the row. As I mentioned above the Wix API will look for the ‘_id’ field and use that if it detects an item.
referencedItem: Object | string | Array<Object> | Array<string>
Wow so the referencedItem can be one of 4 things. As we saw above it could be a string containing the ‘_id’ or the item containing the ‘_id’. Now because we have Multiple Items checked we can also insert more than one item and so this argument can also be an Array of ‘_id’ values or and array of items containing unique ‘_id’ values.
Ok so let’s add a new reference into $w(‘#X’) from our $w(‘#Y’) dataset.
Here they are:
Let’s do two inserts. One with a single item and one with multiple items.
First to insert row 4 of data collection Y into data collection X row 1 we need to know the ‘_id’ of row 4 or have an object containing the row data.
Row 4 has an id of “18878bc2-8a0b-4a75-beae-86789d35b652”.
We need to know the target data collection name “X”
We need to know the reference field name “yReferences” (see the Field Key above)
Lastly we need to know the row ‘_id’ that we are inserting this into which is “69efd383-c12f-4ead-902d-3745a0478914”. So our wixData API call looks like this:
let XRowReferringItem = '69efd383-c12f-4ead-902d-3745a0478914';
let YRowReferencedItem = '18878bc2-8a0b-4a75-beae-86789d35b652';
wixData.insertReference('X', 'yReferences', XRowReferringItem, YRowReferencedItem);
Now since we are using a dataset in the main discussion - $w(‘#X’) how do we do this using a dataset?
OK Well since we are simply dealing with items of a dataset we could assume that $w(‘#X’).getCurrentItem() will return the row containing the ‘_id’ = ‘69efd383-c12f-4ead-902d-3745a0478914’; Then we need to get the information from the $w(‘#Y’) dataset to insert into X.
So we could do something like this:
// We can simply use the Object here as it should contain an '_id'
let XRowReferringItem = $w('#X').getCurrentItem();
// We can load all items in the Y dataset and search for the Item(s) we want to insert into X
let YRowsToFilter = $w('#Y').getItems(0, $w('#Y').getTotalCount());
// Filter array. The result that we get will be an array of 0 or more items
let YRowReferencedItems = YRowsToFilter.filter((item) => {
return item.title === 'Y 1';
});
// Only insert if we have fields to enter
if (YRowReferencedItems.length > 0) {
// We can insert these records
wixData.insertReference('X', 'yReferences', XRowReferringItem, YRowRefencedItems);
}
Hopefully this gives you enough information to make use of the other functions to query, replace and remove multiple field references.
Cheers
Steve
@stevendc thank you, I will go and try those and let you know how it goes.