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"
2
Answers
The reason you get a "No data associated with the event" message is because the extension is defined with a 1st gen compatible trigger, but the code is only listening for a 2nd gen compatible trigger. Because of the differences in the request bodies, the data is not properly parsed for use in your function.
This line in your
extension.yaml
file targets afirestore.onCreate
Cloud Function 1st gen:To target an
onDocumentCreated
Cloud Function for 2nd gen, you would have to update it to use the correcteventType
:Now that you have the correct
eventType
there are a couple of other properties to be updated according to theextension.yaml
reference – the value fortype
andproperties.buildConfig.runtime
(instead of usingproperties.runtime
):You should then be able to deploy this resource with your extension.
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:
(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.