'How do I query a firestore document using firebase_admin with get()

I am trying to retrieve a document from firestore using python but I keep seeing a context error. Upon some research I found that the firebase_admin sdk is using async calls and python is not waiting, but with that knowledge I am still unable to successfully call the .get() method on a collection reference. Here is an example:

firebase_admin.initialize_app(cred)
db = firestore.client()
subscription_data = db.collection("subscriptions").document(purchaseToken)
doc = subscription_data.get()

This is the error on the server:

RuntimeError: cannot exit context: thread state references a different context object

I did attempt to use asyncio with no luck.



Solution 1:[1]

From the documentation on getting a document I see this snippet for doing so in async Python code:

doc_ref = db.collection("cities").document("SF")

doc = await doc_ref.get()
if doc.exists:
    print(f"Document data: {doc.to_dict()}")
else:
    print("No such document!")

So it seems like await might be your way out here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen