I want to get the id of the document that trigger the oncreate function but i tried snap.params.id
like below but i am getting undefined.
When i console log this projectId
, i am getting undefined and sometimes throws error and i am using version "firebase-admin": "~5.12.1", "firebase-functions": "^1.0.3"
exports.created = functions.firestore.document('projects/{projectId}')
.onCreate(snap => {
const projectId = snap.params.id;
const project = snap.data();
const notification = {
title: `${project.title}`,
projectId: `${projectId}`
}
});
So how do i get the document id?
Triggers supported in Cloud Functions (2nd gen) All event-driven functions in Cloud Functions (2nd gen) use Eventarc for event delivery. In Cloud Functions (2nd gen), Pub/Sub triggers and Cloud Storage triggers are implemented as particular types of Eventarc triggers.
Collection Path: What is the path to the Cloud Firestore collection where the extension should monitor for changes? For subcollection, the syntax is parent_collection/{parentId}/target_collection . (please note, there is not depublication process on subcollections).
Since you use a Cloud Functions version >= 1.0, events for onCreate
have two parameters as shown below, and you should use context.params
.
exports.created = functions.firestore.document('projects/{projectId}')
.onCreate((snap, context) => {
const projectId = context.params.projectId;
const project = snap.data();
//......
});
See this doc item for more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With