简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM