[英]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.我正在尝试使用 python 从 firestore 检索文档,但我一直看到上下文错误。 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.
经过一些研究,我发现 firebase_admin sdk 正在使用异步调用,而 python 没有等待,但据此我仍然无法在集合引用上成功调用 .get() 方法。 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.我确实尝试过使用 asyncio,但没有成功。
From the documentation on getting a document I see this snippet for doing so in async Python code:从有关获取文档的文档中,我在异步 Python 代码中看到了这样做的片段:
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.所以看起来
await
可能是你的出路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.