Firebase Admin - How to create new child dynamically

Project Information
Hi, I am creating a discord bot using python for a gaming community. The main purpose is to fetch information from users messages and store them under suitable nodes, which will then be queried by someone else for their reference.

Database Tree

Just like you can see that each yearNode will have monthNode and each monthNode will have dayNode.

Code

d = date.today()
year = d.strftime("%Y")
month = d.strftime("%B")
day = d.strftime("%d")
ref = db.reference("/flights/{}/{}/{}".format(year, month, day))
data = ref.get()
l = []
state = False
while not(state):
  try:
    for i in data.keys():
      l.append(i)
      state = True
  except:
    newRef = db.reference("flights")
    g = newRef.get()
    g[f"{year}"][f"{month}"].update({f"{day}":"1"})
    newRef.set(g)

Modules used

  • date from datetime module
  • db from firebase-admin

Problem Identification
The main problem is that this code works when I manually create the database tree format. But when I am supposed to execute the program the next day, it doesn’t work. I have made few changes in lines 9 - 18, where I tried to create a while loop where the loop checks if there is a dayNode, if not create a dayNode in the except conditional. Good thing is the new dayNode actually gets created by the later part of the code in the program, doesn’t gets executed. Therefore, I guess there is a problem with the try except conditional due to which the program enters into a for loop, which doesn’t let the later part of the code unseen.

Thanks.

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