AttributeError:“ CollectionReference”对象没有属性“ set”

时间:2019-05-01 22:25:49

标签: python-3.x firebase google-cloud-firestore

我正在尝试从Firestore数据库中检索数据,然后进行一些更改,然后再次在数据库中再次设置这些数据。

import firebase_admin
from PreProcessing import Pre_Processing
import pandas as pd
from FeatureExtraction import FeatureExtraction
from Processing import Processing
from firebase_admin import firestore , credentials
def Classification(Data , ID ):
    Answers = []
    Message = Data['message']
    Answers.append(Data['message'])
    PreProcessing = Pre_Processing(Answers)
    Answers = PreProcessing.MainFunction()
    del Answers[0]
    Features = FeatureExtraction(Answers)
    Answers = Features.Test_TFIDF()
    print(Answers.shape)
    print("Finished TF-IDF Training")
    Data1 = pd.DataFrame(Answers)
    del Answers
    Data1 = Data1.fillna(0)
    SentimentList = Features.Sentiment()
    Data1[315477] = SentimentList       
    Processing_Object  = Processing(Data1)
    Results = Processing_Object.Testing()
    Classified = db.collection("Chat").document(Data['ChatID']).collection("chatting").set({
        "message" : Data['message'],
        "from" : Data['from'],
        "Classification" : Results,
        "time" : Data['time'],
    })
    print(Results)
cred = credentials.Certificate("ssmproject-61dec-firebase-adminsdk-op5bp-d525c0a76e.json")
firebase_admin.initialize_app(cred)

db = firestore.client()

NeedClassification = db.collection("NeedClassification")

docs = NeedClassification.get()
for doc in docs:
    if doc != None:
        data = doc.to_dict()
        Classification(data , doc.id)

那是输出

Test.py:39: DeprecationWarning: 'Collection.get' is deprecated:  please use 'Collection.stream' instead.
  docs = NeedClassification.get()
(1, 315477)
Finished TF-IDF Training
Traceback (most recent call last):
  File "Test.py", line 43, in <module>
    Classification(data , doc.id , db)
  File "Test.py", line 25, in Classification
    Classified = db.collection("Chat").document(Data['ChatID']).collection("chatting").set({
AttributeError: 'CollectionReference' object has no attribute 'set'

我只是在将数据设置回数据库时遇到问题 在此先感谢

1 个答案:

答案 0 :(得分:2)

CollectionReference具有add()。

DocumentReference具有set(),update()和delete()。

请参阅:

如果要创建新文档,则代码如下。(文档ID是自动设置的)

    Classified = db.collection("Chat").document(Data['ChatID']).collection("chatting").add({
        "message" : Data['message'],
        "from" : Data['from'],
        "Classification" : Results,
        "time" : Data['time'],
    })

    Classified = db.collection("Chat").document(Data['ChatID']).collection("chatting").document().set({
        "message" : Data['message'],
        "from" : Data['from'],
        "Classification" : Results,
        "time" : Data['time'],
    })