Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing doc.id in .onWrite firebase cloud function

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
          })
        });
like image 784
T_R_U_T_H Avatar asked Aug 25 '18 22:08

T_R_U_T_H


People also ask

How do I get the document ID added to firestore?

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.

Can documents have the same ID Firebase?

The names of documents within a collection are unique.


1 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.

like image 83
Doug Stevenson Avatar answered Oct 23 '22 18:10

Doug Stevenson