我正在使用python3在firestore上查询数据库。我的代码如下:
def getWebConsultReData():
collection = db.collection("webconsult_threescrap").where("compositeKey", "==", "10004-5327729026")
docs = [snapshot.reference for snapshot in collection.stream()]
mDictList = []
print(docs)
for doc in docs:
formattedData = doc.to_dict()
getWebConsultReData()
但是,出现以下错误:
[<google.cloud.firestore_v1.document.DocumentReference object at 0x7f2a183413c8>]
Traceback (most recent call last):
File "<ipython-input-42-172d5765da1d>", line 9, in <module>
getWebConsultReData()
File "<ipython-input-42-172d5765da1d>", line 7, in getWebConsultReData
formattedData = doc.to_dict()
AttributeError: 'DocumentReference' object has no attribute 'to_dict'
我确定是:
该过滤器有效,实际上,以下快照显示了GUI的确切语法
该文档也存在。 有人可以帮我吗?非常感谢!
答案 0 :(得分:3)
您的问题是,在发生错误时,docs
是DocumentReference
的数组,而不是DocumentSnapshot
的数组,因为您将所有其他数据删除了几行向上。 DocumentReference
仅提供文档的路径,不包含文档的全部数据。
要执行等效的操作(收集DocumentReferences
的数组,但还可以访问实际文档),则需要执行以下操作:
def getWebConsultReData():
collection = db.collection("webconsult_threescrap").where("compositeKey", "==", "10004-5327729026")
docs = []
for doc in collection.stream():
formattedData = doc.to_dict()
print(formattedData)
docs.append(doc.reference)
print(docs)
getWebConsultReData()
如果有帮助,您还可以查看如何从集合中获取多个文档的example in the documentation。
答案 1 :(得分:0)
from google.cloud import firestore
def tableconversations(userdata):
docarray = []
db = firestore.Client()
collection = db.collection('tableQuestions').where('userID', '==', userdata['ID']).get()
print("user history matched records",collection)
for doc in collection:
print(doc.to_dict())
docarray.append(doc.to_dict())
return docarray