Description
Invoking an update
or set
operation to write to a Firestore document via the Firestore client does not work when called in a Firestore Emulator Cloud Trigger. Everything works fine when the function is deployed on the cloud.
In the following code example, whenever a document in collection_A
gets updated, an event trigger does the ff:
field_1a
is set totrue
by using the event's document reference (WORKS)field_2a
is set totrue
by using the Firestore client (DOES NOT WORK)- Create a new document using the Firestore client (DOES NOT WORK)
from firebase_admin import firestore
from firebase_functions.firestore_fn import (Change, DocumentSnapshot, Event,
on_document_updated)
from google.cloud.firestore import Client
@on_document_updated(document='collection_A/{document_id}')
def on_document_A_updated(event: Event[Change[DocumentSnapshot]]):
new_doc = event.data.after
data = new_doc.to_dict()
if not data.get('field_1a'):
# WORKS
#
# Using the document reference from the event works fine to update the
# document.
new_doc.reference.update({'field_1a': True})
firestore_client: Client = firestore.Client()
if not data.get('field_2a'):
# DOES NOT WORK
#
# Using a document reference fetched from the client does not work
# to update the document.
firestore_client\
.document(new_doc.reference.path)\
.update({'field_2a': True})
# DOES NOT WORK
#
# Creating a new document does not work.
firestore_client\
.collection('collection_B')\
.document()\
.set({'field_1b': 1})