So close, but I need an expert eye

Hi, my first post, so be kind. I’m developing a web site for my wife’s business (already fraught with risk). She cooks fresh vegan dishes for collection. New ones each week, any left over get frozen or given away (in theory, she launches soon). Using Wix stores, we have a three collections ‘Fresh’, ‘Frozen’ and ‘No Stock’. The latter is for dishes which were previously Fresh or Frozen but which are either out of stock or unsuitable for freezing.

On the shop page we show the Fresh collection in a gallery and then below, the Frozen collection in another gallery.

It is possible there could be no Frozen dishes left, so I want to be able to check whether the Frozen collection is empty (based on if any products are in the collection, not whether the products have non-zero quantity) and then push a message into a text field on the shop page, by the side of the ‘Frozen Meals’ title.

I’ve looked at similar code on the forum and modified this but I’m still getting a compile error. I’m new to JS though many moons ago did cut C, Fortran (yes really) and still play with VBA in Excel, so not completely daft.

Probably a very simple thing, but as we all know these are often the most frustrating…

The text field is ‘frozenNumber’

import wixData from ‘wix-data’ ;

let noFrozenText = “sorry, there are no frozen meals available currently” //initialize variable to display “sold out” text when inventory is 0

$w . onReady ( function () {
//TODO: write your page related code here…
getFrozenCounts ();
});

function getFrozenCounts () {
wixData . query ( “Stores/Frozen” ) //my store collection name
. count ()
. then ( ( num ) => {
let numberOfItems = num ;
if ( numberOfItems === 0 ) {
// no frozen dishes
$w ( “#frozenNumber” ). text = “There are " + num + " frozen dishes available to order” ;
} else {
$w ( “#frozenNumber” ). text = noFrozenText ;
} ;
} );
}

Thanks in advance, Ratty

Hi there! :wave:t2: It looks like you have the if/else statement “backwards”. Right now, if there are no items it shows the “There are X frozen dishes available to order” text, and if there are items it says “there are no frozen meals available currently”.

You should also do a console.log to see if the num variable is really returning something. If not, we’ll have to do more work.

Try switching it to this

function getFrozenCounts() {
    wixData.query("Stores/Frozen") // Store collection name
    .count()
    .then((num) => {
        let numberOfItems = num;

        console.log("Number of Items = " + num) 

        if(numberOfItems === 0) {
            // If there are NO frozen dishes
            $w("#frozenNumber").text = noFrozenText;
        } else { 
            // If there ARE frozen dishes
            $w("#frozenNumber").text = "There are " + num + " frozen dishes         available to order"; 
        };
    });
}

Just noticed there could also be a problem with the database name you are trying to query. If you are querying by collection you need to start with a query of the entire “Stores/Products” database, then find “frozen” in the “collections” field.

wixData.query("Stores/Products") // Wix Stores database
    .eq("collections", "Frozen")  // ("Collections field", "name of collection")
    .count()
    .then((num) => {
    // Etc

Hi LM, ignoring my senior moment schoolboy error with the ‘if’ statement, your second observation appears to have solved the main issue so many thanks.

Unfortunately, having added the console log as suggested the ‘num’ appears to resolve to zero when in fact there are six products currently in the collection.

Is the .count() the best way or is there a better way to determine whether a collection has no products?

Thanks again, Ratty

instead of → .count you also can use → results.length or results.items.length

Okay I figured it out!
After doing some digging, I found that there are restrictions on querying the “collections” field in “Stores/Products”. The basic idea is that you can only search for the collections if you know the _id field.

I have tested the below code on my site and it returns the correct number for each collection. Replace your current function with the async function below.

async function getFrozenCounts() {
    var frozenCollection = (
        await wixData.query('Stores/Collections') 
        // Get the collection from the Wix Collections database
        .eq("name", "Frozen")
        .find()
    ).items[0]

    wixData.query("Stores/Products") // Wix Stores database
        .include("collections")
        .hasSome("collections", frozenCollection._id)
        .count()
        .then((num) => {
            let count = num;
            if (num > 0) {
                // If there ARE frozen dishes
                $w("#frozenNumber").text = "There are " + count + " frozen dishes available to order"; 
                console.log("Number of Items = " + count)
            } else {
                // If there are NO frozen dishes
                $w("#frozenNumber").text = noFrozenText;
                console.log("None = " + count)
            }
        })
}

You can also read more here if you’d like:

Hi LM, that’s fabulous and works a dream. I will also read the articles you’ve posted as I’m keen to learn more Velo. Hope this will be useful to others too.