accessing doc.id in .onWrite firebase cloud function

Viewed 2638

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
          })
        });
2 Answers

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.

Related