My database straight isnt working and im getting zero errors no idea why

from pymongo import MongoClient

from utils.errors import databaseCreateChatPostMissingData

from utils.jsonLoader import read_json

class user_data:

    #Setup class initilization and stuff like that

    def __init__(self, userId, dataIn):

        self.database_name = read_json("ids")['guildDbName']

        self.uid = int(userId)

        self.data = dataIn

        self.db = MongoClient("mongodb://127.0.0.1:27017")[self.database_name]

        self.query = {"_id": int(userId)}

    #Perhaps add a dataparser

    #def dataParser(self):

    #    return 

    #Implmentation/behind scences helper functions not for normal use NEVER USE THESE EXCEPT AS HELPER FUNCTIONS FOR CLASS

    #Makes the new dict for the chat dict to be appended to list

    def __makeChatPostDict(self, messageChannelName: None, messageCurrentUserName: None, messageId: None, messageCreatedTime: None, messageContent: None):

        if messageChannelName is None or messageContent is None or messageId is None or messageCreatedTime is None or messageCurrentUserName is None:

            raise databaseCreateChatPostMissingData

        dct = {

            "content": str(messageContent),

            "channelName": str(messageChannelName),

            "username": str(messageCurrentUserName),

            "time": str(messageCreatedTime),

            "mid": str(messageId)

        }

        return dct

    #This is function responsible for actually updating the data(Split into 2 functions later during refactoring)

    def __assembleChatPost(self, messageChannelName: None, messageCurrentUserName: None, messageId: None, messageCreatedTime: None, messageContent: None):

        try:

            #Setups the collection for chat log assembler along with also setting up the before list of chat data(searchs via user id doesn't include user_id on return tho)

            chat_col = self.db["chat_logs"]

            before_data = None

            raw_chat_logs = None

            before_data = chat_col.find_one({"_id": self.uid}, {"_id": 0, "server_chat_data": 1})

            for x in before_data:

                print(x["server_chat_data"])

            raw_chat_logs = []

            #Makes the new chat logs with the proper extra data appeneded

            updated_chat_logs = raw_chat_logs.append(self.__makeChatPostDict(self, messageChannelName, messageCurrentUserName, messageId, messageCreatedTime, messageContent))

            chat_col.update_one(self.query, {"$set": {"server_chat_data": updated_chat_logs}}, upsert = True)

            return True

        except:

            return False

    #Setup Non-Private Access methods

    def dbChatServerPost(self):

        d = self.data

        return self.__assembleChatPost(d["messageChannelName"], d["messageCurrentUserName"], d["messageId"], d["messageCreatedTime"], d["messageContent"])

    def testRead(self):

        chat_col = self.db["chat_logs"]

        for thingy in chat_col.find({"_id": self.uid}, {"_id": 0, "server_chat_data": 1}):

            print(thingy)

user_chat_log_test_dict = {"messageChannelName": "test", "messageCurrentUserName": "test", "messageId": "test", "messageCreatedTime": "test", "messageContent": "test"}

test = user_data(1, user_chat_log_test_dict)

test.dbChatServerPost()

test.testRead()

this is my python code and i confirm my server is receiving the connections

This blanket try/except is hiding bugs in your code. Remove it and you will find the problem:

        except:
            return False
1 Like

I removed it and its due to being none type
File ‚Äúdatabase.py‚ÄĚ, line 60, in
test.dbChatServerPost()
File ‚Äúdatabase.py‚ÄĚ, line 50, in dbChatServerPost
self.__assembleChatPost(d[‚ÄúmessageChannelName‚ÄĚ], d[‚ÄúmessageCurrentUserName‚ÄĚ], d[‚ÄúmessageId‚ÄĚ], d[‚ÄúmessageCreatedTime‚ÄĚ], d[‚ÄúmessageContent‚ÄĚ])
File ‚Äúdatabase.py‚ÄĚ, line 38, in __assembleChatPost
for x in before_data:
TypeError: ‚ÄėNoneType‚Äô object is not iterable
im using the database to store logs of messages as a dict(id, log[dict(logs)])
but im nto sure why its none type code is same except i removed try catch

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.