I think the code is quite understandable. I have a checkboxgroup with the categories of my products, and I’m trying to filter the list (products are displayed into a repeater) when people change the options in the checkbox group.
I’m doing a query with hasAll to a categories, but including the referenced field “product” so I’m getting al the products related to the categories. BUT: I can’t bind the results into the repeater, and also I’m getting this weird error saying that I need a unique id wich I HAVE. (see the caption)
PS: In the screenshoot attached you can see a product, but it’s not the result of the filtering process.
PS2: I don’t know why I’m getting that code from youtube. I’m just linking the video with the URL in my database.
Any idea? I would really appreciate it.
import wixData from 'wix-data';
$w.onReady(async function () {
let chckOptions = [];
//Getting the product categories
await wixData.query('categorias').find().then(res => {
let items = res["_items"];
//Making the options for checkboxgroup
for (var i = 0; i < items.length; i++) {
chckOptions.push({ "label": items[i]["title"], "value": items[i]
["_id"] });
}
}).catch((err) => {
console.log("Error when trying to load categories: ", err);
});
//Setting the options into the checkboxgroup
$w("#checkboxGroup1").options = chckOptions;
});
export async function checkboxGroup1_change(event) {
//When you choose or change an option into the checkboxgroup
await FilterByCatergory();
}
export async function FilterByCatergory() {
let categories = [];
// Get the indexes of all the checkboxes checked in that group
let selectedCategories = $w("#checkboxGroup1").selectedIndices;
// Now, loop through the checked items and add each city to the Categories
array
if (selectedCategories.length > 0) {
for (var i = 0; i < selectedCategories.length; i++) { categories.push($w('#checkboxGroup1').options[selectedCategories[i]].value);
}
//Getting all the products related to the categories[] array (options choosed from user)
wixData.query("categorias")
.hasAll("_id", categories)
.include("productov2")
.find()
.then((results) => {
if (results.items.length > 0) {
let dataRep = [];
for (var j = 0; j < selectedCategories.length; j++) {
let producto = results.items[j]["productov2"];
dataRep.push(producto);
}
//Printing the data to see if I have the products array in console
console.log("DATA: ", dataRep); // --> See the caption attached
//Trying to set the data into the repeater with no results
$w('#repeater1').data = dataRep;
} else {
console.log("No results");
}
})
.catch((err) => {
let errorMsg = err;
console.log("Error while searching categories: ", errorMsg);
});
} else {
//Need to do something here like a message showing no results, no big deal.
}
}
So basically the value that the reference field holds is the id of the referenced entry. This means that you just have to match the reference field with the _id field of the referenced database.
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#include include
Includes referenced items for the specified properties in a query’s results. Description
The include() function refines a query so that the items returned in the query’s results include the full referenced items for the specified properties.
For example, suppose you have a books collection with an author field that references an authors collection. Querying the books collection with an include(“author”) returns the relevant book items and each item will include the full referenced author item in the book’s author property.
Querying a collection that contains reference fields without using the include() function returns items that contain only the ID of the referenced item, and not the full referenced items.
When including a property with multiple references, the following limitations apply:
Only one property with multiple references can be included.
The query will return an error if more than 50 items are returned, regardless of any query limit set using the limit() function.
Each returned item can include up to 50 referenced items. If there are more than 50 referenced items, only 50 are returned when the query is run and the partialIncludes property of the returned WixDataQueryResult is true. To retrieve more than 50 referenced items, use the queryReferenced() function.
I’m sorry GOS. I think I understand what you mean, but I don’t know how to solve it. Can you be more specific?
That’s my simple collection model. It doesn’t allow me to choose a reference field, just the collection that I want to reference.
And in the final picture the error says about having an _id field and bla bla. I know about the hidden _id property in collections, I’m using it always to make queries. But as you can see in the screenshoot, there’s no duplicate items or id’s or something like that.
Any idea?
I’m feeling so dumb right now, but is better a dumb who ask and learn.
In the first image the console output appears to show an array where the ID is at array[0][0][‘_id’]. It’s no good there. You need IDs for repeater items to be at array[n][‘_id’].