I'm trying to access a document in Firestore like this:
from google.cloud import firestore
def check_stuff(document_id, update_time):
firestore_client = firestore.Client()
doc_ref = firestore_client.collection(u'TestCollection').document(document_id)
print(f"Ref OK")
document = doc_ref.get()
print(f"Doc OK")
if document.exists:
return True
else:
return False
It prints the first print ("Ref OK") but not the following one. Instead I get this error, which I don't really understand. I seems to comes from the get() function itself:
Exception on / [POST] Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/init.py", line 171, in view_func function(data, context) File "/workspace/main.py", line 110, in main check = check_stuff(document_id, update_time) File "/workspace/main.py", line 87, in check_stuff document = document_ref.get() File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/document.py", line 370, in get data = _helpers.decode_dict(document_pb.fields, self._client) File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/_helpers.py", line 318, in decode_dict return {key: decode_value(value, client) for key, value in value_fields.items()} File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/_helpers.py", line 318, in return {key: decode_value(value, client) for key, value in value_fields.items()} File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/firestore_v1/_helpers.py", line 276, in decode_value value_type = value._pb.WhichOneof("value_type") AttributeError: _pb
I checked that the collection truly exists, as well as the document in it. No problem here.
My code is located on a Google Cloud Function, called by Firestore Trigger (write
trigger on the same document). The code works locally on my machine but not on GCP. It doesn't fail every time. It seems to fail only with certain document_id (even if they exist).
Python is 3.7 and google-cloud-firestore==2.0.1
source https://stackoverflow.com/questions/73079130/firestore-python-get-function-to-access-a-document-fails-in-gcp-cloud-functi
Comments
Post a Comment