.eq is not filtering when value is undefined (a BUG maybe?)

I don’t know if this is a bug because it’s shouldn’t be like that at least this is what I think!

Here is the code!

export async function checkIfProductDownloadable(orderId) {
    const options = {
        suppressAuth: true
    };

    return wixData.query("Stores/Orders")
        .eq("_id", orderId)
        .find(options)
        .then((requiredOrderData) => {
            const requiredOrder = requiredOrderData.items;

            if (requiredOrder.length > 0) {
                const now = new Date().getTime();
                const orderDate = requiredOrder[0]._dateCreated.getTime();
                const difference = now - orderDate;
                const oneYear = 31556952000;

                if (difference > oneYear) {
                    return 403;
                } else {
                    return 200;
                }
            }
        })
        .catch((err) => {
            console.error(err);
        })
}

In here I’m checking if users order in the last one year or not. And I’m returning 200 (OK) or 403 (Forbidden) as a result for my Router page. I’m using this backend function in a router.js to check orderId.

What I found is if orderId is undefined it returns 200 (I tested in live mode and also in backend preview test) and .eq should not work like that I don’t know if it’s useful in different cases but this is what I think.

Is this a BUG or .eq just works like that?

You can do something like:

export async function checkIfProductDownloadable(orderId) {
if(!orderId){
	return 403
} else {
	return wixData.query("Stores/Orders")
///...etc..
}
//etc..

I already created a solution something like this:

.eq("_id", orderId != undefined ? orderId : null)

And it’s working but my question is why .eq works like that it should not return when the value undefined it should throw error I guess.

@loeix apparently it ignores undefined values. Maybe it’s a bug, maybe it’s intended. I can’t tell (but I can see the benefit in ignoring undefined values).

But I guess that since an undefined value never exists in the collection, it gets ignored intentionally (no need to run a query if you know the answer upfront).

And by the way, that’s why my code is better than your solution. It saves an unnecessary query (and query time).