Search code examples
firebaseemberfire

Firebase custom unique ID


In my EmberFire app the URL looks like this:

http://example.com/friends/-Jaxji0depwt4PE8KnFG

Is there a way to customize the unique ID in Firebase to look like this:

http://example.com/friends/1
http://example.com/friends/2
http://example.com/friends/3

Solution

  • The unique id in firebase is generated when we push data.

    Example:

    var messageListRef = new Firebase('https://samplechat.firebaseio.com/friends');
    var newMessageRef = messageListRef.push();
    newMessageRef.set({ 'name': 'fred', 'email': '[email protected]' });
    // We've appended a new message to the message_list location.
    var path = newMessageRef.toString();
    // path will be something like
    // 'https://samplechat.firebaseio.com/friends/-IKo28nwJLH0Nc5XeFmj'
    

    Instead, we can directly assign value to child

    Example:

    var friendcount = 1;
    var myFirebaseRef = new Firebase("https://samplechat.firebaseio.com/friends");
    myFirebaseRef.child(""+friendcount).set({'name':'f'+friendcount,'email':'e'+friendcount+'@yh.com'});
    friendcount = 2;
    myFirebaseRef.child(""+friendcount).set({'name':'f'+friendcount,'email':'e'+friendcount+'@yh.com'});
    friendcount = 3;
    myFirebaseRef.child(""+friendcount).set({'name':'f'+friendcount,'email':'e'+friendcount+'@yh.com'});
    

    So, the answer is to use the value directly in child

    myFirebaseRef.child(1).set({'name':'f1'});
    myFirebaseRef.child(2).set({'name':'f2'});
    myFirebaseRef.child(3).set({'name':'f3'});