Back

Technologies:

javascriptjavascript
typescripttypescript
avatar
Tolerim
3 hours ago

How can I monitor modifications to a document and its sub-collections in Firebase Cloud Functions?

I am trying to trigger a function whenever a document is updated in either the parent collection or its sub-collections. Although I followed the instructions mentioned in the Firebase documentation, the function only responds when a doc in a sub-collection is updated, not when a doc in the parent collection is modified.

// Listen for changes in all documents in the 'users' collection and all subcollections
exports.useMultipleWildcards = functions.firestore
.document('users/{userId}/{messageCollectionId}/{messageId}')
.onWrite((change, context) => {
});
Here's the code I'm using:

export const generateAllBooks = functions.firestore
  .document('books/{bookId}/{variantsCollectionId}/{variantId}')
  .onWrite(async (change, context) => {});
Answers(1)
avatar
Tolerim
3 hours ago
Verified Answer
The code you provided is listening for changes in all documents in the 'books' collection and all subcollections, so it should trigger when a doc is changed in either the parent collection or any subcollection. If it's not triggering for changes in the parent collection, make sure that there are actually changes happening in the parent collection and that your function is deployed and active. You can also check the Firebase Function logs to see if there are any errors or if the function is successfully running.
;