0

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.

David Davis
  • 23
  • 1
  • 5

1 Answers1

2

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Yeah I saw and tried that snippet, I will try again and respond. – David Davis May 03 '22 at 23:59
  • No go on the await, I'm using django and the code doesn't seem to fire when using async – David Davis May 04 '22 at 02:28
  • I believe I am going to go the route of scheduling a task to handle updating firestore. I'm going to setup django q and see where that leads me. I think my biggest issue is I'm trying to perform an async task inside of a synchronous view. I attempted to make the view async but that leads to more errors. – David Davis May 04 '22 at 13:46
  • Quick update. I am getting a bad signature error when trying to use async_task from the djangoq library. I generated a new url safe secret_key and applied that with not luck. Weird thing is the code works locally in development, but not on the production server. This problem is insane! – David Davis May 05 '22 at 17:38