Can I get the nth element of a firebase "request"?

Firebase has some basic query functions with orderBy* , limitTo* , startAt() , etc. Is there a way to tell Firebase that you want to get the 10th query result? For example, I use orderByKey() . Since keys are these amazing magic strings, you cannot use integers to refer to any position within them. I would like to store a pointer to a location in the keys and navigate through it. I want orderByKey() and arbitrarily get the key N. Is this possible?

+2
source share
2 answers

While you cannot access child elements by index using Firebase, you can save the element key and use it to run the next query.

 var ref = new Firebase('https://yours.firebaseio.com/items'); var lastKnownKey = null; var firstQuery = ref.orderByKey().limitToFirst(100); firstQuery.once('value', function(snapshot) { snapshot.forEach(function(childSnapshot) { lastKnownKey = childSnapshot.key(); }); }); 

Now you have the lastKnownKey variable, which has the last key you have ever seen. To get the following batch of children, you pass this value to startAt() :

 var nextQuery = ref.orderByKey().startAt(lastKnownKey).limitToFirst(100); 
+4
source

Note that in combination with @ frank-van-puffelen's answer, you can also use the shallow argument to query the top level.

I don’t know how this translates to the JavaScript firebase API, but with curl it will be something like:

 curl 'https://your-site.firebaseio.com/questions.json?shallow=true' 

which returns something like:

 { "-Ju2tGTo6htY2e4mbuPO": true, "-Ju3AWjZnhnUw_OfGyk4": true, "-JughjjzbFOxjevE2ykY": true, "-Jw3cciI6ZpoK1ejfK58": true, "-Jw4NhcgJ9DnenBVphyq": true, "-JwE5ojQ5ZjkvTzVK9E2": true, "-JwE7Qbpf9r1YN8Qaoss": true, "-JwFIQ3pGMCI0E3xzPIz": true, } 

Then, as soon as you receive your small list of items, you can request them one at a time in any order by contacting the key directly:

 curl 'https://your-site.firebaseio.com/questions/-Ju2tGTo6htY2e4mbuPO.json' 
+2
source

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


All Articles