cuzureau
cuzureau

Reputation: 380

Firestore/Python - get() function to access a document fails in GCP Cloud Function

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

Upvotes: 0

Views: 895

Answers (1)

Salman Shaikh
Salman Shaikh

Reputation: 331

Try this one:

from google.cloud import firestore
    
    def check_stuff(document_id, update_time):
        db = firestore.Client()
        doc_ref = db.collection(u'TestCollection').document(u'{document_id}')
        print(f"Ref OK")
        document_status = doc_ref.get()
        print(f"Doc OK")
    
        if document.exists:
            return True
        else:
            return False

Just try to use .where and the filter queries

https://cloud.google.com/firestore/docs/query-data/listen

Or Get Reference of this one

# Create an Event for notifying main thread.
callback_done = threading.Event()

# Create a callback on_snapshot function to capture changes
def on_snapshot(doc_snapshot, changes, read_time):
    for doc in doc_snapshot:
        print(f'Received document snapshot: {doc.id}')
    callback_done.set()

doc_ref = db.collection(u'cities').document(u'SF')

# Watch the document
doc_watch = doc_ref.on_snapshot(on_snapshot)

Upvotes: 1

Related Questions