Skip to content
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

Moment.js does not work for date queries #753

Closed
jakeleventhal opened this issue Sep 7, 2019 · 6 comments · Fixed by #879
Closed

Moment.js does not work for date queries #753

jakeleventhal opened this issue Sep 7, 2019 · 6 comments · Fixed by #879
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jakeleventhal
Copy link

Environment details

  • OS: Linux b8b01391ee38 4.9.125-linuxkit Fix export documentation. #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 GNU/Linux (this is from the node:latest docker image)
  • Node.js version: 12.9.0
  • @google-cloud/firestore version: 2.2.8

Steps to reproduce

  1. Construct a query on a date field field and pass a moment.js object in as the filter value
firestore.collection('Users').where('JoinedDate', '>=', moment().subtract(1, 'day'))
  1. You will get an error that looks like this:
Error: Value for argument "value" is not a valid Firestore document. Couldn't serialize object of type "Moment". Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).

Moment.js is such a popular library used for dates that i think an exception should be made for this.

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Sep 8, 2019
@schmidt-sebastian
Copy link
Contributor

@jakeleventhal Sorry to hear about your trouble.

For now, we only special-case JavaScript's Date (as well as our own Timestamp object). You can convert a moment() date to a JS Date via moment().toDate(). We might consider native support in the future, but would likely need more user feedback/demand before doing so.

@schmidt-sebastian schmidt-sebastian added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Sep 10, 2019
@jakeleventhal
Copy link
Author

jakeleventhal commented Sep 10, 2019

Yeah, we currently do that. It just seems like an unnecessary step. Wouldn't the fix be as simple as adding the following to the where clause check? I was going to make the PR myself but i didn't want to waste my breath if you guys aren't okay with it.

if (typeof val === 'object' && val.constructor.name === 'Moment') {
   val = val.toDate();
}

I understand not wanting to add special case checks for esoteric object types - it's just that moment is used in so many projects.

@schmidt-sebastian
Copy link
Contributor

I talked to some of the folks internally, and we are a little hesitant about adding first party support, especially given existing proposals to add better time management to the Node standard library (such as https://github.com/tc39/proposal-temporal). We can still keep this open as a feature request, which will allow us to gather more feedback.

@bcoe
Copy link
Contributor

bcoe commented Sep 13, 2019

perhaps a good compromise would be to add some samples (time permitting) that demonstrate using firestore in conjunction with moment? I personally love moment, but agree that it's probably a bit too opinionated of a dependency to pull in directly.

@schmidt-sebastian
Copy link
Contributor

Just to clarify - we could accept it as an input type even without a dependency. I don't think we would ever return it from a DocumentSnapshot though (which might be a reason to also not accept it as input).

@jakeleventhal
Copy link
Author

jakeleventhal commented Sep 14, 2019

Right @schmidt-sebastian, @bcoe the sample code i provided above is all that you would need. Just a check on the string type to see if the object is of type "Moment"

@schmidt-sebastian schmidt-sebastian self-assigned this Jan 14, 2020
@google-cloud-label-sync google-cloud-label-sync bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants