提问者:小点点

Firebase“创建”触发器的云函数-多个集合


我在根上创建了3个集合,即品牌,包装和; 项。 我可以保持“创建”函数对每个集合同时。


共2个答案

匿名用户

是的,只需使用onCreate触发器创建三个不同的通配符函数

exports.BrandFunction = functions.firestore
  .document('brands/{brandId}')
  .onCreate(async (snap, context) => {
//Function doesnt need to be async unless you use await in the body
//YOUR CODE HERE 
});

exports.PackFunction = functions.firestore
  .document('packs/{packId}')
  .onCreate(async (snap, context) => {
//YOUR CODE HERE 
});

exports.ItemFunction = functions.firestore
  .document('items/{itemId}')
  .onCreate(async (snap, context) => {
//YOUR CODE HERE 
});

匿名用户

请注意,您可以使用任意多个通配符来替换显式集合或文档ID,例如:

exports.BrandFunction = functions.firestore
    .document('{collectionId}/{docId}')
    .onCreate(async (snap, context) => {
        console.log(context.params.collectionId);
        console.log(context.params.docId);

        return null;

    });

请参阅以下文档:https://firebase.google.com/docs/functions/firestore-events#wildcards-parameters