I’m trying to build a “similar products” gallery with a reference to another item in the collection using its item ID. The .get() function lets me define an item “relatedItem” with the results but I’m having trouble using the defined item “relatedItem”.
My goal is to just identify an item ID in one of the fields of my current item, then pull up its frontImage value as the source for one of my gallery images.
The current item has image fields where I’ve manually put images (as shown by the other 3 image slots in the gallery).
The bolded part is where it gives me an undefined variable error.
Any guidance would be much appreciated.
import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
Hi,
Note that the wixData.get function returns a Promise . In order to resolve it, I would try to set the $w(’#relatedStylesGallery').items inside the wixData.get function, as demonstrated below:
Hey,
You can use a reference field in case you have database relations.
Click here to learn more Reference Fields.
Click here to learn more about How to Create a Reference Field.
Reference Fields are referring to items in different Collections. I’m trying to refer to items in the same Collection. Is there an approach you can recommend for this?
Right I understand the _ID attribute is the best way to go about it, my question is how would I go about implementing that? Is there a general example you can provide?
I mentioned earlier that I just copied/pasted the _ID attribute from one item to another.
Copied ID from Style 1758
Into the Related Style 1 ID field of Style 1757
I then try to call up the ID using this, but it does not work ( bolded ):
import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
Hey,
You can find a general information about how to use the function in the documentation of the get function.
Another option is using the query function:
import wixData from 'wix-data';
wixData.query("ProductPages")
.eq("_id", item.relatedItem1Id)
.find()
.then( (results) => {
let firstItem = results.items[0];
})
.catch( (err) => {
let errorMsg = err;
});
Should the issue persists, please send us the site URL and the page to which the code was added so that I can have a look.
Moreover, note that you can debug the code to better understand what causing the issue.
Hey,
It seems like you were trying to add an HTTP URL to your secured site. Note that you can use only HTTP S when adding HTML component to your site. Click here to learn more About SSL and HTTPS.
The issue here is that the error is referring to image assets in the collection that was placed using the Wix Content Manager. I don’t have control over the URL. Is this an issue on the Wix-end?