Reputation: 183
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 [email protected]
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 [email protected]
I need the Keys viz -Kb5... : which is seen in below Json
"EInfo" : {
"-Kb5UunYfq-8zd_Jc2Nd" : {
"EID" : "0134",
"Email" : "[email protected]",
"Name" : "Parker"
},
"-Kb5V9xHhzVix6BNRM78" : {
"EID" : "1024",
"Email" : "[email protected]",
"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 [email protected]
Upvotes: 0
Views: 1761
Reputation: 598847
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).
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
Upvotes: 3