我的项目中有一个 Firestore 数据库,并且想要读取/写入它而不是 ReatlTime Firestore 数据库。我复制了连接到实时数据库但不是 firestore 数据库的databaseURL。当我生成 firebaseConfig 对象时,它不包含 databaseURL 属性。如何访问 firestore 数据库?
这是我的代码:
import firebase_admin
from firebase_admin import credentials, db
cred = credentials.Certificate("../../../serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://<project-id>-default-rtdb.firebaseio.com/'
})
# Now you can interact with the database
ref = db.reference('testName')
print(ref.get())
我将 python3 版本升级到 3.12.7 并重新安装了 firebase-admin 并将代码更改为此并且它有效:
from firebase_admin import credentials
from firebase_admin import firestore
# Use a service account.
cred = credentials.Certificate("../../../serviceAccountKey.json")
app = firebase_admin.initialize_app(cred)
db = firestore.client()
users_ref = db.collection("test")
docs = users_ref.stream()
for doc in docs:
print(f"{doc.id} => {doc.to_dict()}")