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_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
- shops/600/calls/2_1
- And inside 2_1 I just some fields with test values.
Cloud run trigger set up
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
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));
}
);
{
"dependencies": {
"firebase-functions": "^4.0.0",
"firebase-admin": "^11.0.0",
"axios": "^1.4.0",
"form-data": "^4.0.0"
}
}
{
"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:
Please help me, I struggled this issue for 2 days till 2am