I have this code to filter a query using hasSome() and Date objects
const hasSomeDate = new Date(2022,5,1,15,0);
console.log('hasSome date is ',hasSomeDate);
wixData.query('bookingCollection')
.hasSome("dateTimeSlot", hasSomeDate )
.find()
.then((results) => {
console.log(results);
})
.catch((error) => {
return error;
});
But it always returns 0 items results.items when I try either a single Date or array of Date objects in hasSome()
Here is screen shot of what the query results is like without the hasSome(). It looks like the search value and field value matches but no result when using hasSome(). What am I doing wrong? The same hasSome() works fine on other fields.
I tried using an array and I got the same result. 0 results.items. I have used the array value successfully with texts but I cannot get any results with Date objects
Also I have tested hasSome() with single text ( non array values ) on other collection fields and it returns results items
The API documentation suggests the value doesn’t have to be an array or am I reading this wrong?
This console.log might be misleading as it only logs at the second level, where the actual date value is in milliseconds.
If you want to be sure the value matches you should compare at the ms level.
Something like:
@tony-brunsman This is how I read it : value can be a single string OR single number OR single Date OR Array of any type. My goal is to use it with arrays but I am using a single Date object now just to test my values first which is why I’m having this I’m asking this.
The same Date object when used with .eq() works fine and returns results, so I don’t understand whats wrong here
@avinashmurthy Hi there, multiple values do not need to be in an array. You can have an array of a single item.
Here is what I think you’re looking for and I think it is what @anthonyb was getting at… two objects are never equal when compared. This includes Date objects. When you’re using hasSome(), you are checking if one or more of the object(s) in the array equal some other value (in this case a Date object). This will always return false… unless you are comparing (as anthony mentioned) the ms of the Date object by calling getTime().
You could consider storing the date as a Number instead of a Date object – when you insert a tuple into the collection, just store the returned value of getTime()… that way you can use hasSome() as you have here.
When you retrieve the tuple and you want the Number back into a Date object, just construct a new Date(integerInMS).
@maxbjork Hi, Thanks for your reply. I actually was using a date value like ‘10/10/2022’ initially. And also timestamps. I decided to move to Date objects to avoid conversions in code, because hasSome() allowed it according to the API. But if they will never be equal why would it be in the API?
I am aware I can have single values as I’ve used hasSome successfully before , but just not with dates. But thanks for that info.
Also, I have posted about this to the wix support and after recreating it my ticket was escalated to the developers. Now I’m even more confused if this is a technical fault or something I’m doing wrong. But anyways I’ll leave it at that. And try other ways to code the solution if I don’t get a working response from them