Firebase retrieves data using orderByKey () and equalTo ()

So, I am 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. What happened? What should i use?

+6
source share
1 answer

In the sample data you provided in <<20> there is no node with the key 54ca2c11d1afc1612871624a .

Your keys are -Jh46tlaNFx_YmgA8iMJ and -Jh474kAvoekE4hC7W3b . You can easily determine this yourself:

 ref.child("studentList").orderByKey().on("child_added", function(snapshot) { console.log(snapshot.key()); // on newer SDKs, this may be snapshot.key }); 

It seems you want to order nodes by their value, but this is not the operation that is currently available on Firebase. Firebase can request children by the value of a named property , key, or its priority . Therefore, if you change the data structure to:

 disney studentList -Jh46tlaNFx_YmgA8iMJ: name: "aaaaabbbbbbaaaa" -Jh474kAvoekE4hC7W3b: name: "54ce1cbec4335cd3186105dc" 

You can get the childrenโ€™s order by name:

 ref.child("studentList") .orderByChild("name") .equalTo("54ca2c11d1afc1612871624a") .on("child_added", function(snapshot) { console.log(snapshot.val()); }); 

Only the side of the node: if your actual node values โ€‹โ€‹are similar to those specified in the example you provided, you might want to use the values โ€‹โ€‹as keys; they already seem quite unique to my unprepared eye.

+16
source

Source: https://habr.com/ru/post/981832/


All Articles