简体   繁体   中英

AttributeError: 'CollectionReference' object has no attribute 'set'

I am trying to retrieve data from my Firestore Database and then Make some changes and then set these data again in the database again.

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)

That is the output

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'

I just have problem in Setting the Data back to the Database Thanks in Advance

CollectionReference has add().

DocumentReference has set(), update() and delete().

See:

If you want to create new document then the code is like this.(document id is auto set)

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM