0

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

1
  • Can you try collection(...).stream() and print the documents you receive? Commented Jul 27, 2022 at 3:51

1 Answer 1

1
+100

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)
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is actually bigger and more complex to me. See here : stackoverflow.com/questions/73223848/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.