-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug with useCollection and where by timeStamp. #51
Comments
Yeah...this is a known issue. At the moment, only serializable fields are valid, since this is how we store items in the cache. It seems like #46 might be a solution to this, where you could convert your date string back into a date object in the |
On second thought, looks like that wouldn’t exactly solve this, it’s only for data manipulation. I’d have to give some thought into this. Does firestore allow any date string comparison for queries? |
Yeah either you can convert the string Date back into a Date object or you can store the Dates as strings in firestore and query based on string Dates. You could also use Unix timestamps. |
I tried a bunch of different conversations to get it working and couldn't get it. You might have better luck than I. Maybe we can change the api to something like this: where: ['time', '<', {type: 'date', value: currentDate.unix()}] or where: ['time', '<', currentDate, {type: 'date'}] |
Could you store your dates as Unix time stamps and then use that to query? |
I could, but I have a lot of data with firebase.Timestamp in it. I didn't want to have to deal with drift between clients. |
Got it. Something like this, like you proposed, seems like it would make sense: where: ['time', '<', {type: 'date', value: currentDate.unix()}] It would still check if it’s just a value, but if there’s an object, it would parse it accordingly. I’m not sure if there would be any odd edge cases to an api change like that. |
I think this would probably be safer, you don't have to refactor code that checks where: ['time', '<', currentDate, {type: 'date'}] |
I think that makes sense to me. Would you like to contribute this in |
@nandorojo Why was this closed? The following code does not work, it simply returns an empty array always.
However, querying directly from the firestore instance works:
My dates are stored as timestamps in the database. Also, why do you need to change the syntax like you mentioned, if the original firestore instance queries perfectly fine with Date objects? |
I can re-open if someone wants to submit a PR with this solution. Otherwise, I'll try to get to it when I have time. |
I can try implementing it, but I would like to know the reason we cannot just query with the Date object, like firestore does? Is it because of swr cache like you mentioned earlier? Could we not use some kind of serializer / deserializer for date objects when storing in cache instead? It seems odd that the API for this package would differ from the firestore instance API. |
It's because this library has to stringify the query before passing to SWR in order to have a stable key. Maybe we could check if it's a date object before serializing and inject a |
I personally think this is the better solution. If the user specifically provides a |
Yeah I'm thinking the API is, there is a Then, when creating the firestore ref, we check if we have The user doesn't have to inject anything in that case, but if they happened to, it wouldn't make a difference. |
@nandorojo I have now submitted a PR (#75) which implements a proper serializer which will allow us to easily serialize any kind of object in the future. Please take a look! |
I have also noticed that you cannot query for document references. For example:
This is most likely for the exact same reason. I will add support for serializing document references to the PR. |
Hello @AlexanderArvidsson, I'm currently stuck with date filtering, and your PR would be handy! What is the current status? Thank you. |
@binajmen I am already using my fork in production. It's up to @nandorojo to accept the pull request. There is still a few improvements that can be done in the pull request (check the comments), but I do not have time to dive into it. |
@AlexanderArvidsson now that you've been using it in prod, are you confident in the PR's ability to merge? |
@nandorojo While I have been using it in prod without any problems so far, I have two concerns:
However, these concerns shouldn't be too critical regardless. If you wish to merge, then that is fine. You could create a task to look into the second concern I had. |
I am also having the same issue. Luckily found this issue quickly. Any fix is highly appreciated! Thanks. 😀 |
Hello, any workarounds that I can query the timestamp by range at this moment? |
Sorry.
const date = new Date(moment().add(-7, "days").format());
const Component = () => {
const { data } = useCollection("users", {
where: ["createdAt", ">=", date],
});
return <div></div>
} |
That’s because the date gets recreated on each render, so it would fetch every time. You probably want this: const date = useRef(new Date(…)).current and put that inside your component. but yeah, currently this only works with a milliseconds time stamp as this issue states. |
@nandorojo |
Scratch that, missed reading the strike throughed part that moving it outside would solve it. |
Love using this lib so far. I am trying to make a paginated list of messages for a messaging app, and I think I stumbled on a bug. Because of the JSON serialization / maybe something else in useCollection,
where: ['time', '<', new Data(Date.now())]
doesn't return anything, but a regular get on a collection ref does.The text was updated successfully, but these errors were encountered: