Attempting to query collections based on a specific products collection

Hi, I am trying to create a related products repeater on the products page that queries products that are in the same collection as the product as well as the same price range. My issue is that I am unable to pull the name or id of the collection that the product is a part of. This is because the ‘collections’ field is a multi-reference field. I have attached my code below.

async function relatedProductsByPrice(product) {
// Get the current product’s ID.
let productId = product._id;
let productCollection = product.collections._id; ******ISSUE IS HERE

let relatedByPrice = await wixData.query( ‘Stores/Products’ )
.include( “collections” )
.hasSome( “name” , productCollection)
.between( ‘price’ , product.price * 0.7 , product.price * 1.3 )
.ne( ‘_id’ , productId)
.find();
return relatedByPrice.items;
}

If there is any way to pull the ‘name’ or ‘_id’ field for the collection then I can query easily. I am not sure if it is a syntax mistake or if I need to change my method.

You didn’t write how you got this product in the first place (how you passed it to the function. If you didn’t use .include() to get this product so obviously the reference field won’t be there.

Hi JD, so my code is quite long for the products page which is why I didn’t post it all. I have attached an example of my products page below where you can see that there are two repeaters. The first repeater shows all products where the name of the products starts with the same name as the main product. This repeater is working fine. The second repeater is the one I am struggling with. I am able to find the products that are within the same price range but I’m not able to show only products in the same collection (in this case “loveseats”).

https://auppal33.wixsite.com/website-2/product-page/creeal-heights-loveseat-1

The code below is the query function for the first repeater. This is working perfectly.

async function relatedProductsBySeries(product) {
// Get the current product’s ID.
let productId = product._id;
let productTitle = product.name;
let productSplit = productTitle.split( ’ ’ ).slice( 0 , 1 );
productTitle = productSplit[ 0 ];

// Query the “Products” collection for product’s whose price is within 20% of the current product’s price.
let relatedBySeries = await wixData.query( ‘Stores/Products’ )
.startsWith( ‘name’ , productTitle)
.ne( ‘_id’ , productId)
.find();
return relatedBySeries.items;
}

The code posted above is the code used in the second repeater. That is where Im having issues. Hopefully this clears it up. I have also attached my full code if you want to take a look.

You need to use .include(“collections”) in the query if you’re looking for the reference field values.

Hi JD, I have the .include(‘collections’) in the query but the issue still remains that I don’t have a way of checking which collection the product is in.

async function relatedProductsByPrice(product) {
// Get the current product’s ID.
let productId = product._id;
let productCollection = product.collections.id;
console.log( "id: " + product.collections.id)

let relatedByPrice = await wixData.query( ‘Stores/Products’ )
.include( “collections” )
.hasSome( “id” , productCollection)
.between( ‘price’ , product.price * 0.7 , product.price * 1.3 )
.ne( ‘_id’ , productId)
.find();
return relatedByPrice.items;

}

I have tried using the note that they have for the collections field that says there is a hidden field collections.id but no results

But you’re trying to run this line you still don’t have the referenced value:

let productCollection = product.collections.id; 

And if you don’t have the value, the code won’t do what you want it to do.

What do you suggest then? iv tried the same approach but putting product.collections.id directly in the query but that didnt work either.

async function relatedProductsByPrice(product) {
// Get the current product’s ID.
let productId = product._id;
//let productCollection = product.collections.id;
//console.log("id: " + product.collections)

let relatedByPrice = await wixData.query( ‘Stores/Products’ )
.include( “collections” )
.hasSome( “id” , product.collections.id)
.between( ‘price’ , product.price * 0.7 , product.price * 1.3 )
.ne( ‘_id’ , productId)
.find();
return relatedByPrice.items;

}

I only see part of your code and I don;t really know what you’re trying to achieve, so I can’t make suggestions. But first of all you need to get the specific product with include() and only then you can use it.
In your last post to trued to query .hasSome(“id”, product.collections.id)) without having it first. Think about it. if you don’t have the product.collections.id before this query, how can you use it in the query?