The issue with Wix Stores databases. Need help!

I want to filter products by their categories so I created some collections from the dashboard and I added some products to these new collections (categories).

So here is what I did and what is the problem:

I created a dropdown input and automated the options. All options are showing the categories (Store collections) the label and the value (value is the id of each collection) and here is the code of how I did it:

export function getCategories() {
    return wixData.query("Stores/Collections")
        .find()
        .then((results) => {
            let data = results.items;
            let options = new Array();
            data.shift()

            data.map((item) => {
                options.push({ "label": item.name, "value": item._id });
            })

            return options;
        })
}

//I shift the first index because I don't need All Products category on dropdown so it just removes the `All Products option`

Then I switched to front-end and added some code and here it is:

import { getCategories } from 'backend/Book-Explore/book-explorer.jsw'
import wixStorage from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {
    initPage()
});

async function initPage() {
    setupDropdown();
    exploreBook()
}

function setupDropdown() {
    getCategories()
        .then((results) => {
            $w("#bookCategories").options = results;
        })
}

function exploreBook() {
    $w("#searchBook").onClick((event) => {
        let selectedCategory = $w("#bookCategories").value;
        wixStorage.session.setItem("selectedCategory", selectedCategory);
        wixLocation.to("/kitap-kesfetme/");
    })
}

In here as you can see I’m setting the options of dropdown and using session storage for storing the selection of users. And when they click the search button they will be redirected to another page. On this page, I will create a repeater and show related products to the selected category.

I’m facing a problem right now until now everything works and is fine.

First I checked the “Stores/Products” database manually and I saw the collections field which has multiple references (so ids) and I wrote my simple code to filter product by ids but this is what I see in fact what I can’t see is. I can’t see the collections field when I return all the products.


So as you can see there is no “collections” field. I found an article about “Stores/Products” and in the article, it says you can filter it etc. (this article). But the problem is I can’t see the collections field I can’t filter it I can’t return it. I tried to setFilter with datasets also it does not work so I tried every single step to filter products by collections but I can’t.

My client wait for this site and it should be ready (I will create one more site by the same date) by 2 December. But I can’t find any solution to this issue.

If I can’t filter this field or I can’t see it let me know because as I know I can do it. And I should be able to do it.

Hi Enes Bekar,
Collections field is a Multi-Reference Field in “Stores/Products” collection.
So you must use .include(“collections”) function in the code.
https://www.wix.com/velo/reference/wix-data/wixdataquery/include

import wixData from 'wix-data';

$w.onReady(function () {
    wixData.query("Stores/Products")
        .include("collections")
        .find()
        .then((results) => {
            console.log(results.items)
        })
});

I also tried this before but let me try again

@loeix It will work for sure. But remember collections field is a reference. So, the result will be in Array. Like this

[
  {
    "_id": Collection ID,
    "name": Collection Name,
    "mainMedia": CollectionImage
  },
  { //Example
    "_id": "600655b5-63e1-e491-fedb-777a0bd6f0b8",
    "name": "Sale",
    "mainMedia": "wix:image://v1/c22c23_527e6c6f3d944fb88c80907c2f754afb~mv2.jpg/file.jpg#originWidth=2000&originHeight=2000"
  }
]

Result in console

@workiapps Yes also this is what I was looking and it worked thanks again :slight_smile: