Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Firestore Triggers not Detecting Write to Database

OS: Mac OS Catalina v 10.15.1

Python version: Python 3.7.1

I'm writing a Cloud Functions in Python to be triggered by writes/updates/etc. to a Firestore database. My function essentially just looks like this in my main.py:

def hello_firestore(data, context):
    """ Triggered by a change to a Firestore document.
    Args:
        data (dict): The event payload.
        context (google.cloud.functions.Context): Metadata for the event.
    """
    print("Hello")

I want this function to watch for updates to the document test_collection/test_document. I deployed this function using:

gcloud functions deploy hello_firestore \
  --runtime python37 --trigger-event providers/cloud.firestore/eventTypes/document.write \
  --trigger-resource projects/PROJECT_ID/databases/default/documents/test_collection/test_document

The function deploys successfully, and appears in my Firebase console and in the GCP console. I have a script running on my local machine:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import time

cred = credentials.Certificate("./path/to/adminsdk.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

doc_ref = db.collection(u'test_collection').document(u'test_document')
doc_ref.set({u'test_field':'test_value'})

time.sleep(60)

doc_ref.update({u'test_field':'new_value'})

The database updates accordingly. I expect the function to execute (since the trigger condition is satisfied), and I would expect to see the corresponding stdout output in the Firestore console logs or the GCP console logs. However, there is nothing there -- no error message, and no indication of the trigger event happening (the only logs are the ones that arise as a result of deploying the function). Additionally, manually editing the database in the Firestore console does not have an effect.

I've also tried writing this Cloud Function in Node.js (and deploying it accordingly) and the result is the same -- no indication that the event has occurred.

Any tips/suggestions would be great. Thanks!

like image 274
krt24 Avatar asked Jan 17 '26 11:01

krt24


1 Answers

I believe you want the following deploy command, where default is in parenthesis:

gcloud functions deploy hello_firestore \
  --runtime python37 --trigger-event providers/cloud.firestore/eventTypes/document.write \
  --trigger-resource projects/PROJECT_ID/databases/(default)/documents/test_collection/test_document

See here for more details: https://cloud.google.com/functions/docs/calling/cloud-firestore

like image 129
Dustin Ingram Avatar answered Jan 20 '26 04:01

Dustin Ingram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!