Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Firebase when using push() How do I pull the unique ID

I'm attempting to add/remove entries from a Firebase database. I want to list them in a table to be added/modified/removed (front end) but I need a way to uniquely identify each entry in order to modify/remove. Firebase adds a unique identifier by default when using push(), but I didn't see anything referencing how to select this unique identifier in the API documentation. Can this even be done? Should I be using set() instead so I'm creating the unique ID?

I've put this quick example together using their tutorial:

<div id='messagesDiv'></div> <input type='text' class="td-field" id='nameInput' placeholder='Name'> <input type='text' class="td-field" id='messageInput' placeholder='Message'> <input type='text' class="td-field" id='categoryInput' placeholder='Category'> <input type='text' class="td-field" id='enabledInput' placeholder='Enabled'> <input type='text' class="td-field" id='approvedInput' placeholder='Approved'> <input type='Button' class="td-field" id='Submit' Value="Revove" onclick="msgRef.remove()">  <script> var myDataRef = new Firebase('https://unique.firebase.com/');    $('.td-field').keypress(function (e) {     if (e.keyCode == 13) {       var name     = $('#nameInput').val();       var text     = $('#messageInput').val();       var category = $('#categoryInput').val();       var enabled  = $('#enabledInput').val();       var approved = $('#approvedInput').val();       myDataRef.push({name: name, text: text, category: category, enabled: enabled, approved: approved });       $('#messageInput').val('');     }   });   myDataRef.on('child_added', function(snapshot) {     var message = snapshot.val();     displayChatMessage(message.name, message.text, message.category, message.enabled, message.approved);   });   function displayChatMessage(name, text, category, enabled, approved, ) {     $('<div/>').text(text).prepend($('<em/>').text(name+' : '+category +' : '+enabled +' : '+approved+ ' : ' )).appendTo($('#messagesDiv'));     $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;   }; </script> 

Now lets assume I have three rows of data:

fred : 1 : 1 : 1 : test message 1 fred : 1 : 1 : 1 : test message 2 fred : 1 : 1 : 1 : test message 3 

How do I go about uniquely identifying row 2?

in the Firebase Database they look like this:

-DatabaseName     -IuxeSuSiNy6xiahCXa0         approved: "1"         category: "1"         enabled: "1"         name: "Fred"         text: "test message 1"     -IuxeTjwWOhV0lyEP5hf         approved: "1"         category: "1"         enabled: "1"         name: "Fred"         text: "test message 2"     -IuxeUWgBMTH4Xk9QADM         approved: "1"         category: "1"         enabled: "1"         name: "Fred"         text: "test message 3" 
like image 267
Front_End_Dev Avatar asked May 19 '13 16:05

Front_End_Dev


1 Answers

To anybody finding this question & using Firebase 3+, the way you get auto generated object unique ids after push is by using the key property (not method) on the promise snapshot:

firebase   .ref('item')   .push({...})   .then((snap) => {      const key = snap.key    }) 

Read more about it in the Firebase docs.

As a side note, those that consider generating their own unique ID should think twice about it. It may have security and performance implications. If you're not sure about it, use Firebase's ID. It contains a timestamp and has some neat security features out of the box.

More about it here:

The unique key generated by push() are ordered by the current time, so the resulting list of items will be chronologically sorted. The keys are also designed to be unguessable (they contain 72 random bits of entropy).

like image 176
Dan Mindru Avatar answered Sep 27 '22 00:09

Dan Mindru