Wix SDK Error when trying to put items into a repeater - Weird stuff to such a simple task.

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.
}
}

As you are going by the reference field, you need to go by the ID as stated in the error message.

https://support.wix.com/en/article/about-reference-fields-in-database-collections
The value that is displayed in the reference field comes from the Main field of the specific item in the referenced collection. The actual value that is stored in the database is the ID of the referenced item (in other words, the value in the ID system field of the referenced collection).

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://support.wix.com/en/article/about-your-database-collection-fields#system-fields
Field Name - ID
Field Key - _id
Field Type - Text
Description - A unique identifier for the item. You can assign the ID a value when you import new data from a CSV file. Otherwise the ID is a random UUID.

Using Corvid by Wix you can also assign the ID a value when adding items with the Data API.
Once defined the ID cannot be edited.

https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#hasAll
hasAll
If the value of the specified property is an array, hasAll() will match if there is a match in the elements of that array for all of the specified values.

https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#hasSome
hasSome
If the specified property contains multiple references, pass item IDs in the value property. In such a case, hasSome() will match if any of the multiple references match any of the specified ID values.

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.

See previous forum posts for more info.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-wix-data-multiple-references
https://www.wix.com/corvid/forum/community-discussion/query-using-a-multi-reference-field
https://www.wix.com/corvid/forum/community-discussion/retrieving-multiple-item-reference-field-using-code

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.

Is there any place to check “most common wix sdk errors” or something like that? I still can’t get it.

Up

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’].

Lee, You helped me in two lines. I solved the problem and finished the project. Thank you SO MUCH .