1
votes

I am trying to invoke a Cloud Function using the Cloud Firestore trigger. Basically, my cloud function is taking a full export of my sub collection 'reviews' whenever a new document is added to 'reviews'. I have around 6-7 documents inside my 'reviews' sub collection. I deployed the function through the console and the deployment completed. However, this function is not getting triggered whenever I add a new document to my 'reviews' sub collection. Can someone please tell me what could be the issue?

Trigger type: Cloud Firestore (Beta) Event type: providers/cloud.firestore/eventTypes/document.write Document Path: test_restaurant/{reviews}

enter image description here

EDIT: I want my cloud function to only export the new documents added to my firestore to my GCP bucket. Below is the function I am trying to write:

const functions = require('firebase-functions');
const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();

const bucket = 'gs://ABC/trigger_test'
exports.newFirestoreBackup = functions.firestore
  .document('test_restaurant/{Id}/reviews/{Id}') 
  .onWrite((change, context) => {
   const databaseName = client.databasePath(
   // process.env.GCLOUD_PROJECT,
   "FS-123",
    '(default)'
  );
return client
    .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      // Leave collectionIds empty to export all collections
      // or define a list of collection IDs:
      // collectionIds: ['users', 'posts']
      collectionIds: ['reviews'],
    })
    .then(responses => {
      const response = responses[0];
      console.log(`Operation Name: ${response['name']}`);
      return response;
    })
    .catch(err => {
      console.error(err);
    });
  
  
  });

CONSOLE SNIPPET: enter image description here

1
Can you pls share the entire code of your Cloud Function? - Renaud Tarnec
Edited the question with my cloud function code @Renaud Tarnec - nsk

1 Answers

1
votes

You don't share any code, but if you want your Cloud Function to be triggered "whenever [you] add a new document to the reviews subcollection", it should be declared with the following path:

exports.createUser = functions.firestore
    .document('test_restaurant/{restaurantId}/reviews/{reviewId}')
    .onCreate((snap, context) => {...});

You could use an onWrite() trigger as well, with the same path.