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;
});
});
#relatedrepeater #database #dynamicpage
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
@givemeawhisky Actually i have dynamic page with repeater items not a store website i’m implementing this code but their is one error & i’m not able to understand why it is not taking database.
Here is my code
import wixData from 'wix-data';
$w("#relatedData").onReady(() => {
let related = $w("relatedData").getCurrentItem().category;//error "not a valid selector"
wixData.query("category")
.eq("approved", true)
.eq("category", related)
.limit(8)
.find()
.then((results) => {
$w("#relatedRepeater").data = results.items;
});
});
Once it will error-free i hope the related item repeater will work
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...
Dataset - Velo API Reference - Wix.com
Dataset - Velo API Reference - Wix.com
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.
@givemeawhisky one confusion the code runs successfully but my category field is text & the related repeater is on dynamic page so in category field i mention the categories like in text so its not showing related items on page what can i do should i change the field type or anything else. here is my code:
import wixData from 'wix-data';
$w.onReady(() => {
$w("#relatedData").onReady(() => {
let related = $w("#relatedData").getCurrentItem().category;
wixData.query("category")
.ge("category")
.eq("approved", true)
.eq("category", related)
.limit(8)
.find()
.then((results) => {
$w("#relatedRepeater").data = results.items;
});
});
});