Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 885
Mohamed Nashaat Avatar asked Oct 29 '25 19:10

Mohamed Nashaat


1 Answers

CollectionReference has add().

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

See:

  • https://googleapis.github.io/google-cloud-python/latest/firestore/collection.html
  • https://googleapis.github.io/google-cloud-python/latest/firestore/document.html
  • https://googleapis.github.io/google-cloud-python/latest/firestore/index.html

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'],
    })
like image 51
zkohi Avatar answered Oct 31 '25 08:10

zkohi