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

change before and after data should not be undefined for onUpdate #659

Closed
sk- opened this issue Apr 11, 2020 · 7 comments · Fixed by #670
Closed

change before and after data should not be undefined for onUpdate #659

sk- opened this issue Apr 11, 2020 · 7 comments · Fixed by #670

Comments

@sk-
Copy link
Contributor

sk- commented Apr 11, 2020

The change argument of the handler for onUpdate should probably be Change<QueryDocumentSnapshot> instead of Change<DocumentSnapshot>, so that change.after.data() and change.before.data() return a non undefined object.

See https://github.com/firebase/firebase-functions/blob/master/src/providers/firestore.ts#L205-L212

If that's not the case, please clarify when either of those can be actually undefined.

[REQUIRED] Version info

firebase-functions: 3.6.0

[REQUIRED] Expected behavior

change.before.data() and change.after.data() should have a type of DocumentData

[REQUIRED] Actual behavior

change.before.data() and change.after.data() have a type of DocumentData | undefined

@google-oss-bot
Copy link
Collaborator

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@mbleigh
Copy link
Contributor

mbleigh commented Apr 12, 2020 via email

@sk-
Copy link
Contributor Author

sk- commented Apr 12, 2020

@mbleigh i know about the restrictions for onWrite, but I'm talking about onUpdate.

@sk- sk- changed the title change before and after data should not be undefined change before and after data should not be undefined for onUpdate Apr 12, 2020
@sk-
Copy link
Contributor Author

sk- commented Apr 12, 2020

@mbleigh the same is true for onCreate, and onDelete. snapshot should have a type of QueryDocumentSnapshot.

@sk-
Copy link
Contributor Author

sk- commented Apr 12, 2020

If anyone needs to augment firebase-functions's types, you could use the following:

import {firestore} from 'firebase-functions';

import {Change, CloudFunction, EventContext} from 'firebase-functions';
import * as firebase from 'firebase-admin';

declare module 'firebase-functions/lib/providers/firestore' {
  export type QueryDocumentSnapshot = firebase.firestore.QueryDocumentSnapshot;

  interface DocumentBuilder {
    /** Respond only to document updates. */
    onUpdate(handler: (change: Change<QueryDocumentSnapshot>, context: EventContext) => PromiseLike<any> | any): CloudFunction<Change<QueryDocumentSnapshot>>;
    /** Respond only to document creations. */
    onCreate(handler: (snapshot: QueryDocumentSnapshot, context: EventContext) => PromiseLike<any> | any): CloudFunction<QueryDocumentSnapshot>;
    /** Respond only to document deletions. */
    onDelete(handler: (snapshot: QueryDocumentSnapshot, context: EventContext) => PromiseLike<any> | any): CloudFunction<QueryDocumentSnapshot>;
  }
}

I'd be happy to add this to a PR.

@laurenzlong
Copy link
Contributor

Hey @sk- this is a great suggestion! When we first started supporting Firestore, QueryDocumentSnapshot was not introduced. If you can make the PR that would be very appreciated, I'd be happy to review.

sk- added a commit to sk-/firebase-functions that referenced this issue Apr 24, 2020
The arguments of Firestore triggers `onCreate`, `onUpdate` and `onDelete` cannot have an undefined data by definition, so we changed the arguments of those handlers to reflect that by using DocumentQuerySnapshot.

fixes firebase#659
@sk-
Copy link
Contributor Author

sk- commented Apr 24, 2020

@laurenzlong great, I just sent #670

laurenzlong pushed a commit that referenced this issue Apr 27, 2020
…#670)

The arguments of Firestore triggers `onCreate`, `onUpdate` and `onDelete` cannot have an undefined data by definition, so we changed the arguments of those handlers to reflect that by using DocumentQuerySnapshot.

fixes #659
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants