javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-extensions

How to read the event when a Cloud Function is triggered in a Firebase Extension?


I'm trying to follow the docs regarding get started with a Firebase Extension using Firebase Functions Firestore 2nd-gen. Inside my extension.yaml file I have created a resource:

resources:
  - name: onDocCreate
    type: firebaseextensions.v1beta.function
    description: >-
      A function that fires when a new document is created.
    properties:
      eventTrigger: 
        eventType: providers/cloud.firestore/eventTypes/document.create
        resource: projects/${PROJECT_ID}/databases/(default)/documents/docs/{docId}
      runtime: "nodejs18"

Inside my index.js file, I have added these lines of code:

const {
  onDocumentCreated,
} = require("firebase-functions/v2/firestore");

exports.onDocCreate = onDocumentCreated("docs/{docId}", (event) => {
  const data = event.data;
  if (!data) {
    console.log("No data associated with the event");
    return;
  }
  const docId = event.params.docId;
  console.log("docId: " + docId);
});

When I a dd a new document in the "docs" collection I always get:

No data associated with the event

As per my understanding, the onDocumentCreated fires but with no data. If I comment the first 5 lines and I try to log the docId, I always get:

docId: undefined

How solve this issue?


resources:
  - name: onDocCreate
    type: firebaseextensions.v1beta.function
    description: >-
      A function that fires when a new document is created.
    properties:
      eventTrigger: 
        eventType: google.cloud.firestore.document.v1.created
        resource: projects/${PROJECT_ID}/databases/(default)/documents/docs/{docId}
      runtime: "nodejs18"
      platform: "gcfv2"

Solution

  • firebaser here

    Firestore triggers for v2 functions are currently not documented, but should work.

    As suggested in some other responses, the resource type for v2 functions must be firebaseextensions.v1beta.v2function. You can use some other documented v2 triggert as an example here: https://firebase.google.com/docs/extensions/publishers/functions#crashlytics

    Also, the resource filter format is different for v2 functions. It relies on Eventarc event filtering, so you need to use event filter syntax. Some available Firestore filters are documented here: https://cloud.google.com/functions/docs/calling/cloud-firestore#deploy_the_hello_firestore_function

    Also, event filter syntax for v2 functions is documented here: https://cloud.google.com/functions/docs/reference/rpc/google.cloud.functions.v2#eventfilter

    So, perhaps try something like this:

    resources:
      - name: ondoccreate
        type: firebaseextensions.v1beta.v2function
        description: >-
          A function that fires when a new document is created.
        properties:
          buildConfig:
            runtime: nodejs18
          eventTrigger:
            eventType: google.cloud.firestore.document.v1.created
            triggerRegion: nam5
            eventFilters:
              - attribute: database
                value: (default)
              - attribute: document
                value: docs/{docId}
                operator: match-path-pattern
    

    (also note the function naming requirements for v2 functions, upper case letters are not supported)

    EDIT: added the required triggerRegion. You might want to make it a parameter for users to control, at least until the extensions platform adds a way automatically infer it.