Python 3.x “DocumentReference”对象没有“to_dict”属性
我正在使用python3查询firestore上的数据库。我的代码如下:Python 3.x “DocumentReference”对象没有“to_dict”属性,python-3.x,google-cloud-firestore,Python 3.x,Google Cloud Firestore,我正在使用python3查询firestore上的数据库。我的代码如下: def getWebConsultReData(): collection = db.collection("webconsult_threescrap").where("compositeKey", "==", "10004-5327729026") docs = [snapshot.reference for snapsho
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的确切语法
文件也存在。
有人能帮我吗?多谢各位 您的问题是,在出现错误时,docs是一个documentreference数组,而不是documentsnapshot数组,因为您只需几行就删除了所有其他数据。DocumentReference只提供文档的路径,它不包含文档的全部数据 要做一些等效的事情来收集一系列DocumentReference,同时还可以访问实际文档,您需要执行以下操作: def getWebConsultReData: collection=db.collectionwebconsult\u threescrapt.wherecompositeKey,==,10004-5327729026 文档=[] 对于collection.stream中的单据: formattedData=doc.to_dict 打印格式化数据 docs.appenddoc.reference 打印文档 getWebConsultReData
如果有帮助,您还可以查看如何从一个集合中获取多个文档的方法。您的问题是,在出现错误时,文档是一个DocumentReference数组,而不是DocumentSnapshot数组,因为您只需几行即可删除所有其他数据。DocumentReference只提供文档的路径,它不包含文档的全部数据
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
要做一些等效的事情来收集一系列DocumentReference,同时还可以访问实际文档,您需要执行以下操作:
def getWebConsultReData:
collection=db.collectionwebconsult\u threescrapt.wherecompositeKey,==,10004-5327729026
文档=[]
对于collection.stream中的单据:
formattedData=doc.to_dict
打印格式化数据
docs.appenddoc.reference
打印文档
getWebConsultReData
如果有帮助,您还可以查看如何从集合中获取多个文档的详细信息
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