问题
I have the below Json schema , what firebase query can I use to get the node name(0153,0154) , I am able to get the name(joel,vikram,sachin) using once method
"EmployeeInfo":
{
"0153":
{
"Name":"Joel",
"Dept":"Engineering",
"Email":"joel@dept.com"
},
"0163":
{
"Name":"Vikram",
"Dept":"Engineering",
"Email":"vikram@dept.com"
},
"0173":
{
"Name":"Sachin",
"Dept":"Engineering",
"Email":"Sachin@dept.com"
}
Here is my javascript function :
function Call() {
var dbref = new Firebase("https://logintrialapp.firebaseio.com/Employee/EmployeeInfo");
var login = localStorage.getItem("Email");
dbref
.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childData = childSnapshot.val();
name = childData.Name;
var n = login.localeCompare(name);
console.log(name);
if (n != 0) {
var eid = snapshot.val();
console.log("Hello" + eid);
}
console.log(snapshot.val());
})
})
with this code I am able to get the names but I want to retrieve the node names , what query can be used to get the nodes names based on emailid match criteria
output:
Joel
Checkworks.html:50 Hello[object Object]
Checkworks.html:55 Object {0153: Object, 0163: Object, 0173: Object}0153: ObjectDept: "Engineering"Email: "joel@dept.com"Name: "Joel"__proto__: Object0163: Object0173:
Object__proto__: Object
Checkworks.html:44 Vikram
Checkworks.html:50 Hello[object Object]
Checkworks.html:55 Object0153: ObjectDept: "Engineering"
Email: "joel@dept.com"Name: "Joel"__proto__: Object0163
: Object0173: ObjectDept: "Engineering"Email: "Sachin@dept.com"Name: "Sachin"__proto__: Object__proto__: Object
Checkworks.html:44 Sachin
Checkworks.html:50 Hello[object Object]
Checkworks.html:55 Object {0153: Object, 0163: Object, 0173: Object}
回答1:
To get the key of the childSnapshot call childSnapshot.key:
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childKey = childSnapshot.key; // Get the key of the child
childData = childSnapshot.val();
name = childData.Name;
var n = login.localeCompare(name);
console.log(name);
if (n != 0) {
var eid = snapshot.val();
console.log("Hello" + eid);
}
console.log(snapshot.val());
})
})
回答2:
There is now a shallow command in the REST API that will fetch just the keys for a path. This has not been added to the SDKs yet.
In Firebase, you can't obtain a list of node names without retrieving the data underneath. Not yet anyways. The performance problems can be addressed with normalization.
Essentially, your goal is to split data into consumable chunks. Store your list of video keys, possible with a couple meta fields like title, etc, in one path, and store the bulk content somewhere else. For '/video_meta/id/link, title, ... /video_lines/id/...'
To learn more about denormalizing, check out this article: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.htm
To get the "name" of any snapshot (in this case, the ID created by push()) just call name() like this:
var name = snapshot.name();
If you want to get the name that has been auto-generated by push(), you can just call name() on the returned reference, like so:
var newRef = myDataRef.push(...);
var newID = newRef.name();
来源:https://stackoverflow.com/questions/41718302/getting-node-names-from-firebase-database-based-on-emailid-match