首页 > 解决方案 > 多个集合的云函数触发器

问题描述

exports.myFunction = functions.firestore
    .document('users/{userID}')
    .onDelete((snap, context) => {
        // do something
    });

我希望这个函数也能触发另一个集合,比如offices. 在不复制和粘贴整个内容的情况下执行此操作的最佳方法是什么?

标签: javascriptnode.jsgoogle-cloud-functions

解决方案


路径中的任何内容都可以是通配符,因此如果要在所有集合上触发:

exports.myFunction = functions.firestore
    .document('{collectionName}/{userID}')
    .onDelete((snap, context) => {
        // do something
    });

但是,无法设置触发两个但不是所有集合的单个路径。如果您愿意,只需通过在 aa(常规非云)函数中隔离该代码来最小化代码重复:

exports.myFunction = functions.firestore
    .document('users/{userID}')
    .onDelete((snap, context) => {
        doSomething(...)
    });
exports.myFunction = functions.firestore
    .document('offices/{officeID}')
    .onDelete((snap, context) => {
        doSomething(...)
    });
function doSomething(...) {
    ...
}

推荐阅读