eja*_*azz 6 firebase google-cloud-functions google-cloud-firestore
我们是一个无模式模型,我想在将文档添加到集合时触发云函数defects。问题是任何都defect可以包含一组新的缺陷集合(递归)。
如何设置在更新/创建以下任何文档时触发的云功能:
problem/defects/{document}
problem/defects/{document}/defects/{document}
problem/defects/{document}/defects/{document}/defects/{document}
problem/defects/{document}/defects/{document}/defects/{document}/defects/{document}
等等...
通过 firestore 函数,您可以使用通配符路径来触发每个可能的路径。但是,您需要为文档的每个级别指定一个触发器(即 - 深度 = 1、深度 = 2、深度 = 3 等)。这是我写的,用于处理最多 4 层深度:
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);
});Run Code Online (Sandbox Code Playgroud)
Cloud Functions 触发器不允许使用跨越多个集合名称或文档 ID 的通配符。如果您需要一个函数在任意数量的路径上触发,则需要单独定义它们,但每个函数可以共享一个公共实现,如下所示:
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1580 次 |
| 最近记录: |