So I'm trying to find the data "aaaaabbbbbbaaaa" in this structure:
disney
studentList
-Jh46tlaNFx_YmgA8iMJ: "aaaaabbbbbbaaaa"
-Jh474kAvoekE4hC7W3b:"54ce1cbec4335cd3186105dc"
I used this
var ref = new Firebase("https://disney.firebaseio.com/");
ref.child("studentList").orderByKey().equalTo("54ca2c11d1afc1612871624a").on("child_added", function(snapshot) {
console.log(snapshot.val());
});
But I got nothing back. What was wrong? What should I use?
I guess it was added after your question was made, the solution here https://firebase.google.com/docs/database/admin/retrieve-data?hl=en#ordering-by-value tackles your need right away:
ref("studentList").orderByValue().on("value", function(snapshot) {
snapshot.forEach(function(data) {
console.log(data.val());
});
});
In the data sample that you've given, there is no node under studenList
with a key of 54ca2c11d1afc1612871624a
.
Your keys are -Jh46tlaNFx_YmgA8iMJ
and -Jh474kAvoekE4hC7W3b
. You can easily determine this yourself by:
ref.child("studentList").orderByKey().on("child_added", function(snapshot) {
console.log(snapshot.key()); // on newer SDKs, this may be snapshot.key
});
You seem to want to order the nodes by their value, but that is not an operation that is available on Firebase at the moment. Firebase can only query children by a value of a named property, the key or its priority. So if you'd change the data structure to:
disney
studentList
-Jh46tlaNFx_YmgA8iMJ:
name: "aaaaabbbbbbaaaa"
-Jh474kAvoekE4hC7W3b:
name: "54ce1cbec4335cd3186105dc"
You could get the children order by name with:
ref.child("studentList")
.orderByChild("name")
.equalTo("54ca2c11d1afc1612871624a")
.on("child_added", function(snapshot) {
console.log(snapshot.val());
});
Just a side node: if your actual node values are like the ones in the sample you provided, you might want to consider using the values as the keys; they already seem pretty unique to my untrained eye.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With