简体   繁体   中英

How to get Key / Id of the data written into Firebase?

I have a application which stores employee records, it stores the employees ID, but also needs each employee to have a local ID, I want to use the ID generated by Firebase when we push data onto the db using push() function as the internal ID for each employee. This is required for change management.

My code:

function submit()
    {


    var dsRef=new Firebase("https://dataretrievaltest-28983.firebaseio.com/EmployeeDB/EInfo");

    var name=document.getElementById('Name').value;
    var eid=document.getElementById('EID').value;
    var email=document.getElementById('Email').value;

    dsRef
                    .push(
                            {
                            Name:name,
                            EID: eid,
                            Email: email,
                            }
                        );

    var pushid=dsRef.name;
    console.log("name:"+pushid);
    console.log(name);
    console.log(eid);
    console.log(email);

    } 

Output of the above code:

name:function (){S("Firebase.name() being deprecated. Please use Firebase.key() instead.");D("Firebase.name",0,0,arguments.length);return this.key()}
savekey.html:104 Djoko
savekey.html:105 0143
savekey.html:106 Djoko@dept.com

Have used Key in place of name as name is deprecated but did not get the key, instead it returns a function.

The code with key in place of name

var pushid=dsRef.key;
console.log("name:"+pushid);

Output after change:

name:function (){D("Firebase.key",0,0,arguments.length);return this.path.e()?null:me(this.path)}
savekey.html:104 Caroline
savekey.html:105 0192
savekey.html:106 caroline@dept.com

I need the Keys viz -Kb5... : which is seen in below Json

"EInfo" : {
      "-Kb5UunYfq-8zd_Jc2Nd" : {
        "EID" : "0134",
        "Email" : "Parker@dept.com",
        "Name" : "Parker"
      },
      "-Kb5V9xHhzVix6BNRM78" : {
        "EID" : "1024",
        "Email" : "sanchez@dept.com",
        "Name" : "sanchez"
      }
}

The available properties(key/name) don't give the required output. Please let me know how to retrieve the key and if I am missing anything.

Also tried

var pushid=dsRef.child("EInfo").push().key;
console.log("name:"+pushid);

it did not give the desired result.

name:function (){D("Firebase.key",0,0,arguments.length);return this.path.e()?null:me(this.path)}
savekey.html:104 camila
savekey.html:105 0172
savekey.html:106 camila@dept.com

You're using Firebase SDK version 2.x, but are mixing in syntax from version 3.x. For Firebase 2.x, the syntax to get the key is:

var pushid=dsRef.key();
console.log("name:"+pushid);

If you're creating this app now, you should really be using Firebase 3.x SDKs, in which case the syntax would indeed be dsRef.key (without parentheses).

Update

You seem to be looking for the key of the item that you just created with push() . You can get that by assigning the result from push to a variable:

var newRef = dsRef.push({
    Name:name,
    EID: eid,
    Email: email,
});
console.log(newRef.key()); // or newRef.key when using SDK version 3.x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM