1

I'm working on a chat project on the platform Raspberry PI 3, Openelec OS.

Trying to write to the DB and getting unwanted anonymous keys.

Firebase real-time DB

Unwanted key marked with yellow. Movie2 and it's keys and values are the wanted result, but I made it manually.

I only ask how can I prevent this anonymous random key to be there and how can I replace it with other key? (string, a movie name for example)

This is my code:

url = 'https://chat-example-97c62.firebaseio.com/Movie1.json'
postdata = {
    'date': str(time.asctime( time.localtime(time.time()) )),
    'temp': str("Hello from Kodi")
 }
req = urllib2.Request(url)
req.add_header('Content-Type','application/json')
data = json.dumps(postdata)

Thanks.

AL.
  • 36,815
  • 10
  • 142
  • 281
Yarin Levy
  • 13
  • 2
  • To ensure the data adheres to the schema you want, you can write `.validate` clauses in the security rules of your database. For more information on that, see: https://firebase.google.com/docs/database/security/ – Frank van Puffelen Jan 23 '17 at 17:02
  • @FrankvanPuffelen OK this is validating. But how do I write to the database without the annonymous key in first place? For example, for the key Movie1 in the picture I want to have only the keys date, username and their values. Without the KbBDD4QDDiZn3it6r8m – Yarin Levy Jan 30 '17 at 20:14

1 Answers1

2

When you send a POST request to Firebase, it automatically generates a Key (Anonymous Key), if you want to use your own key you need to use a PATCH request, this is an example on Python 3:

def update_entry(user, message):

    my_data = dict()
    my_data["user"] = user
    my_data["message"] = message

    json_data = json.dumps(my_data).encode()
    request = urllib.requests.Request("https://<YOUR-PROJECT-ID>.firebaseio.com/movies/<REPLACE_THIS_WITH_YOUR_DESIRED_KEY>.json", data=json_data, method="PATCH")

    try:
        loader = urllib.request.urlopen(request)
    except urllib.error.URLError as e:
        message = json.loads(e.read())
        print(message["error"])
    else:
        print(loader.read())
Mr. Phantom
  • 482
  • 3
  • 11