Hi,
I want to create a related items repeater showing on the dynamic page.
The available field in my database is image, title, button.
& the category field is text with help of category field it shows the related items on the dynamic page.
& this is my code but it's not working
import wixData from 'wix-data';
$w("#collectionName").onReady(() => {
let category = $w("#collectionName").getCurrentItem().category;
wixData.query("category")
.include("category")
.eq("category", category)
.limit(8)
.find()
.then((results) => {
$w("#relatedRepeater").data = results.items;
});
});
Firstly, add your page onReady function to your page too underneath your imports line.
You can have the datasets own onReady function wrapped up in the pages onReady function too.
import wixData from 'wix-data'; $w.onReady( () => { $w("#relatedData").onReady( () => { //rest of code...
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onReady
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem
A dataset needs to load its data before you call its getCurrentItem() function. Usually a dataset finishes loading a short time after the page it is on finishes loading. So if you call getCurrentItem() inside the page’s onReady() event handler, the dataset might not be ready yet.
To call getCurrentItem() as soon as possible after a page loads, use the dataset's onReady() function inside the page’s onReady() event handler to ensure that both the page and the dataset have finished loading.
Get the dataset's current item when the page loads
$w.onReady( () => { $w("#myDataset").onReady( () => { let itemObj = $w("#myDataset").getCurrentItem(); } ); } ); //Returns all current users data //* itemObj: * * { * "_id": "fcec780a-3e37-4b64-8a66-37c552c53f99", * "_owner": "f6c0f9c3-a62d-7e9f-g58d-943829af244d9", * "_createdDate": "2017-05-01T17:19:03.823Z", * "_updatedDate": "2017-05-01T17:19:10.477Z", * "title": "Dr. ", * "name": "B", * "link-dynamic-name": "/myCollection/B" * } *//
As for your error...
let related = $w("relatedData").getCurrentItem().category; //error "not a valid selector"
You have just simply missed out the '#' at the start of the name of your dataset, look at your dataset onReady function for the correct way.
Why not just use the Wix Stores tutorial and simply change it to suit your own dataset.
https://www.wix.com/corvid/example/related-items