Hi, I am looking for help with my code for adding a related items repeater to my store’s product page.
I have basically followed this tutorial:
In my case, ‘productA’ and ‘productB’ are ‘petrol’ and ‘diesel’ .
If the name of the product includes ‘diesel’ then I want to display related products from the ‘petrol’ list, and vice versa.
So as I understand it, if the product displayed on the product page is one of those in the ‘diesel’ column, then the repeater should show the ‘petrol’ products.
I didn’t need to display products that are related by price, so I left that section out.
When I preview the code, I get the following error:
Wix code SDK error: Each item in the items array must have a member named _id
which contains a unique value identifying the item.
It says the error is at line 48, which is this line: $w ( “#relatedItemsRepeater” ). data = relatedProducts ;
I don’t know how to fix it so I would appreciate any help.
disclaimer
I know nothing about coding and have literally copied the tutorial pretty much exactly so please be gentle!!
edit
It would seem others have also had similar problems (I searched) but no answers were forthcoming.
Thank you
Cat
Here is the code:
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
$w . onReady ( function () {
loadRelatedProducts ();
});
async function loadRelatedProducts () {
let product = await $w ( ‘#productPage’ ). getProduct ();
let relatedProductResults = await Promise . all ([
relatedProductsByName ( product )
]);
if ( relatedProductResults [ 0 ]. length > 0 )
showRelatedProducts ( relatedProductResults [ 0 ]);
**else**
showRelatedProducts ( relatedProductResults [ 1 ]);
};
async function relatedProductsByName ( product ) {
let productId = product . _id ;
// find related products by name table
let relatedByName = **await** Promise . all ([
wixData . query ( 'relatedProducts' )
. eq ( 'diesel' , productId )
. include ( 'petrol' )
. find (),
wixData . query ( 'relatedProducts' )
. eq ( 'petrol' , productId )
. include ( 'diesel' )
. find ()
]);
let relatedProducts = [
... relatedByName [ 0 ]. items . map ( _ => _ . diesel ),
... relatedByName [ 1 ]. items . map ( _ => _ . petrol )
];
**return** relatedProducts ;
};
function showRelatedProducts ( relatedProducts ) {
if ( relatedProducts . length > 0 ) {
relatedProducts . splice ( 4 , relatedProducts . length );
$w ( ‘#relatedItemsRepeater’ ). onItemReady ( relatedItemReady );
$w ( “#relatedItemsRepeater” ). data = relatedProducts ;
$w ( “#relatedItems” ). expand ();
} else {
$w ( “#relatedItems” ). collapse ();
}
};
function relatedItemReady ( $w , product ) {
$w ( “#productImage” ). src = product . mainMedia ;
$w ( “#productName” ). text = product . name ;
$w ( “#productPrice” ). text = product . formattedPrice ;
$w ( ‘#productImage’ ). onClick (() => {
wixLocation . to ( product . productPageUrl );
});
};