Manoj Sharma
Manoj Sharma

Reputation: 3

Cloud Functions for Firebase "Create" Trigger - multiple collections

I created 3 Collection on root i.e Brand, Pack & Item in the same project. Can I keep "create" functions against each collection same time.

Upvotes: 0

Views: 553

Answers (2)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

Note that you may use as many wildcards as you like to substitute explicit collection or document IDs, for example:

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

        return null;

    });

See the doc: https://firebase.google.com/docs/functions/firestore-events#wildcards-parameters

Upvotes: 2

barbecu
barbecu

Reputation: 742

Yes, Just create three different wildcard functions with the onCreate trigger

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 
});

Upvotes: 1

Related Questions