0

I'm currently using push as it gives me a randomly ( or quazi-random ) unique identifier.

I'm in a scenario where I would prefer to use updates instead of pushes.

Is there anyway I can ask firebase to genarate a unique if for me? Or would be be best to genarate on client side and if so how would I guard against updating a already existing key?

var updates = {};
updates['model/' + <i_need_new_unique_key_here>] = someVariable;
firebase.database().ref().update(updates);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Snewedon
  • 2,410
  • 2
  • 13
  • 27
  • 1
    When you create an item Firebase generates a unique key using the current time, so they're ordered chronologically. Is there a reason you can't generate an empty item, then push to it later? – Brett DeWoody Aug 05 '16 at 03:00
  • It sounds like you want to check if a given key already exists. If that is the case, see: http://stackoverflow.com/questions/24824732/test-if-a-data-exist-in-firebase. For a more involved solution that enforces uniqueness on the server (by using security rules), see: http://stackoverflow.com/questions/25294478/how-do-you-prevent-duplicate-user-properties-in-firebase – Frank van Puffelen Aug 05 '16 at 13:47
  • @BrettDeWoody i'm updating a lot at once, and all the data is interlinked so wold prefer to do it with one call – Snewedon Aug 05 '16 at 18:29
  • So you already have items created in Firebase? Or you want to push multiple items at once and have Firebase generate unique keys? You may need to update the question with some more details. – Brett DeWoody Aug 05 '16 at 19:46
  • Does ```firebase.database().ref().key;``` make a round trip to google? It dosen't seem like it does. If f not the i Just loop though and create come keys for my updates. – Snewedon Aug 06 '16 at 23:02

0 Answers0