Reputation: 2508
We a schema-less model where I would like to trigger a cloud function when a document is added to a defects
collection. The thing is that any defect
can contain a group of new defects collection (recursive).
How can I setup a cloud function that triggers on any of the following documents is updates / created:
problem/defects/{document}
problem/defects/{document}/defects/{document}
problem/defects/{document}/defects/{document}/defects/{document}
problem/defects/{document}/defects/{document}/defects/{document}/defects/{document}
and so on...
Upvotes: 6
Views: 1867
Reputation: 3383
To solve this error, I looked at the logs in firebase-debug.log
and I saw the following error:
"description": "Expected value chat/{tenant}/{customerId} to match regular expression [^/]+/[^/]+(/[^/]+/[^/]+)*"
If you look at the regex, it's asking for an even number of paths. I updated the path and it worked.
Upvotes: 0
Reputation: 770
With firestore functions, you can use wildcard paths to trigger for every possible path. However, you will need to specify a trigger for each level of document (i.e. - depth=1, depth=2,depth=3, etc...). Here is mine that I wrote to handle up to 4 levels deep:
const rootPath = '{collectionId}/{documentId}';
const child1Path = '{child1CollectionId}/{child1DocumentId}';
const child2Path = '{child2CollectionId}/{child2DocumentId}';
const child3Path = '{child3CollectionId}/{child3DocumentId}';
export const onCreateAuditRoot = functions.firestore
.document(`${rootPath}`)
.onCreate(async (snapshot, context) => {
return await updateCreatedAt(snapshot);
});
export const onCreateAuditChild1 = functions.firestore
.document(`${rootPath}/${child1Path}`)
.onCreate(async (snapshot, context) => {
return await updateCreatedAt(snapshot);
});
export const onCreateAuditChild2 = functions.firestore
.document(`${rootPath}/${child1Path}/${child2Path}`)
.onCreate(async (snapshot, context) => {
return await updateCreatedAt(snapshot);
});
export const onCreateAuditChild3 = functions.firestore
.document(`${rootPath}/${child1Path}/${child2Path}/${child3Path}`)
.onCreate(async (snapshot, context) => {
return await updateCreatedAt(snapshot);
});
Upvotes: 5
Reputation: 317467
Cloud Functions triggers do not allow wildcards that span more than one name of collection or document ID. If you need a function to fire on any number of paths, you will need to define them each separately, but each function can share a common implementation, like this:
functions.firestore.document("coll1/doc").onCreate(snapshot => {
return common(snapshot)
})
functions.firestore.document("coll2/doc").onCreate(snapshot => {
return common(snapshot)
})
function common(snapshot) {
// figure out what to do with the snapshot here
}
Upvotes: 3