Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore access using Cloud Functions event

I'm struggling with a few syntax issues

exports.deleteProject = functions.firestore.document('{userID}/projects/easy/{projectID}').onDelete(event => {

inside the functions I have

console.log(event)

which outputs

 { data: 
   DocumentSnapshot {
     _ref: DocumentReference { _firestore: [Object], _referencePath: [Object] },
     _fieldsProto: undefined,
     _readTime: undefined,
     _createTime: undefined,
     _updateTime: undefined },
  eventId: 'd4079c38-2dc1-44e2-924d-fa27c3a95f8b-0',
  eventType: 'providers/cloud.firestore/eventTypes/document.delete',
  notSupported: {},
  params: 
   { userID: 'xxxxxxxxxxxxxx',
     projectID: 'dddddddddddd' },
  resource: 'projects/nnnnnnnnn/databases/(default)/documents/xxxxxxxxxxxxxx/projects/easy/dddddddddddd',
  timestamp: '2017-11-11T04:41:16.712975Z' }

The problem I have is I can't seem to figure out the syntax to reference the database (and subsequently another collection/document path) itself or the different elements that appear when I print out event.

Any help would be appreciated

like image 206
Blue Avatar asked Dec 22 '25 10:12

Blue


1 Answers

When you receive a Firestore event in Cloud Functions, it's an object of type Event<DeltaDocumentSnapshot>, which means event.data is of type DeltaDocumentSnapshot. With this, you can access the entire database using the received event via event.data.ref.firestore. This gives you a Firestore object that you can use to access collections and documents within that instance of Firestore. For example:

const firestore = event.data.ref.firestore
const docref = firestore.doc('collection/doc')

Also, you can use the Admin SDK (with its Firestore API) to access Firestore directly from any Cloud Function without a Firestore event.

like image 60
Doug Stevenson Avatar answered Dec 24 '25 00:12

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!