"Error: Unsupported filter operator $gte for start"

Hi,
I’m trying to filter a dataset that is using the events collection. The events collection has a Start Date field with a key of “start”. When I try to filter the dataset I get the error mentioned in the title of this post.

Upon checking the data structure for the Events data collection I see that the start date filtering is set to “No”. However, the created date field is set to “Yes” that it can be filtered, but, I receive the same error on that field when using it in my filter function.

I have a try catch statement associated with the filter and the function completes successfully in that the try statement is executed as though there was no error detected. yet an error is reported in the console just after the filter function is run.

Can someone please tell me how I can filter the Events collection using the start and end date fields using JavaScript and WixCode? Below is the simple code snippet from my page. I am trying to use this on the dataset bound to the Events data collection.

$w(“#dataset1”).onReady( function () {

// Sort the dataset (overides the sort implemented with the editor)
$w(“#dataset1”).setSort(wixData.sort()
.ascending(“start”)
)
.then(() => {
console.log(“Dataset is now sorted”);
})
. catch ((err) => {
console.log(err);
});

// set a filter on the dataset for any items where start (date) is greater than today and title contains the string “Choir”
$w(“#dataset1”).setFilter(wixData.filter()
.contains(“title”, “Choir”)
.ge(“start”, dtoday)
)
.then(() => {
console.log(“Dataset is now filtered”);
})
. catch ((err) => {
console.log(err);
});

});

wanna know this too

It looks like the error is balking with your .ge() section. Can you print a console.log of dtoday to confirm what is in it.

Remember that ‘dtoday’ has to be of type date.

So you could try:

let dtoday = new Date();  // set dtoday to current date/time
dtoday.setHours(0,0,0,0);  // set date to beginning of day (00:00 AM)

And, just to confirm you are not using the “start” field to search with. As you had stated that is not a field you can run a query against. Neither “start” nor “end” can be filtered on…

Here is the documentation about the fields:

It seems that the documentation is correct in that you cannot filter on the start nor end date for an event in the events collection. I find it incredible that you can’t search/filter for the events by a date range.

I’ve confirmed with several other developers currently online on discord, that you are correct and that infact you cannot query the events collection as you are trying to do. @bwprado has suggested a work around but you have to do the query on the backend using the ‘wix-events-backend’ api.

Here is a sample test snippet he provided:

 import { wixEvents } from"wix-events-backend"
 
 export const test = async () => { 
     const date = newDate('May 20 2022');
     return await wixEvents.queryEvents()
         .gt('scheduling.startDate', date)
         .find()
 }

Thanks @pekrzyz , let me just make a better function for anyone out there trying to solve this problem.

Create a file on the backend called events-utils.jsw .
Paste this code below on it:

import { wixEvents } from 'wix-events-backend'

export const getAvailableDates = async (startDate, endDate, sorting = 'asc') => {
    const query = wixEvents
        .queryEvents()
        .ge('scheduling.startDate', startDate)
        .le('scheduling.endDate', endDate)

    let sort = {
        asc: query.ascending().find(),
        desc: query.descending().find(),
    }

    return await sort[sorting]
}


On the frontend, just use this code inside your page whenever you need to retrieve the available dates.

import { getAvailableDates } from 'backend/events-utils'

$w.onReady(async () => {
    const startDate = new Date('May 15 2022')
    const endDate = new Date('May 29 2022')
    // You feed the start date and end date, and a sorting string, can be
    // either 'asc' for ascending or 'desc' for descending.
    const availableDates = await getAvailableDates(startDate, endDate, 'asc')

    console.log(availableDates)
})