Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Can not access params in cloud run function 2nd gen from firestore trigger

Please help me. I have used GCP for the first time in 2 days, I have researched for 2 days but no way to resolve this.

I am trying to trigger events update, create and delete from Firestore to Cloud run functions.

But every time I update a value, I only got a set of event of meaningless numbers and an empty params with {} value. What could be the possible problem?

Here is my set up.

Firestore set up:

  • Rules (just a simplest rule for access firestore):

 

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

 

  • Firestore structure

- shops/600/calls/2_1

- And inside 2_1 I just some fields with test values.

Cloud run trigger set up

  • Trigger type: Google sources
  • Event provider: Cloud firestore
  • Event type: google.cloud.firestore.document.v1.written
  • Filters:
    • database: equal (default)
    • document: Operator 2, path pattern: shops/{shop_id}/calls/{call_id}
  • Service account: My own service account.

I ran this gcloud functions to set up policy for service account to access firestore:

 

gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
  --member="serviceAccount:xxxx-compute@developer.gserviceaccount.com" \
  --role="roles/datastore.user"

 

Cloud run code files

  • First is my code (index.js), it is only a very simple code to log event from document. I followed this doc to write the simplest code Cloud firestore triggers 

 

const { onDocumentWritten } = require("firebase-functions/v2/firestore"); // Import 2nd Gen Firestore triggers
const { getFirestore } = require("firebase-admin/firestore"); // Import Firestore Admin
const { initializeApp } = require("firebase-admin/app"); // Import Firebase Admin App
const { logger } = require("firebase-functions/v2");

initializeApp();
const db = getFirestore();

exports.buddyListener = onDocumentWritten(
  "shops/{shop_id}/calls/{call_id}",
  (event) => {
    logger.info("Full event object:", JSON.stringify(event, null, 2));
  }
);

 

  • This is my package.json

 

{
    "dependencies": {
      "firebase-functions": "^4.0.0",
      "firebase-admin": "^11.0.0",
      "axios": "^1.4.0",
      "form-data": "^4.0.0"
    }
  }

 

  • Last is my cloudbuild.json

 

{
    "steps": [
        {
            "name": "gcr.io/google.com/cloudsdktool/cloud-sdk",
            "args": [
                "gcloud",
                "functions",
                "deploy",
                "buddyListener",
                "--gen2",
                "--region=asia-northeast1",
                "--source=.",
                "--trigger-event-filters=type=google.cloud.firestore.document.v1.written",
                "--trigger-event-filters=database=(default)",
                "--trigger-event-filters-path-pattern=document=shops/{shop_id}/calls/{call_id}",
                "--runtime=nodejs20"
            ]
        }
    ]
}

 

 Then I just deploy, everything got done with no errors.

And I try to update some value inside 2_1. It should fire some event with params and data. But after checking the log I got params is {} and data is undefined. Because the data is undefined so when I use JSON.stringify there is no data in my output. 

Below is my output log, because the output event is very long, so I only capture the first and the last lines:

tu_nguyen_0-1736668794704.png

tu_nguyen_1-1736668813736.png

 

Please help me, I struggled this issue for 2 days till 2am

0 0 134