I'm triggering a cloud function with onWrite. Within the onWrite function, I want to do something with the ref ID of the document that was written. How can I grab this? My current code looks something like this, but I don't know how to fill eventID.
exports.syncEvents = functions.firestore
.document('events/{eventID}')
.onWrite((change, context) => {
admin.firestore().collection('users').doc(host).collection('events').doc(eventID).set({
active: true
})
});
When you call the . add method on a collection, a DocumentReference object is returned. DocumentReference has the id field, so you can get the id after the document was created. // Add a new document with a generated id.
The names of documents within a collection are unique.
change.after
is a DocumentSnapshot of the document that changed, after it was written. DocumentSnapshot has a property called id
that contains the id of the document. So change.after.id
is the document id.
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