new_new123
new_new123

Reputation: 31

Firebase filtering results

I have a question I read from realtime database 1000 last records but I need for example record number 100,200,300... can I do this ? I'm going to make chart and I want for example only show there every 10th measurement. My databse looks like that: enter image description here

 firebase.database().ref("Humidity").ref.orderByChild("time").limitToLast(1000).on("child_added",snapshot => {

Upvotes: 0

Views: 80

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

It sounds like you're trying to built offset-based pagination, where you first show the most recent 100 messages, then the next 100, etc. That is not how pagination in Firebase works.

Pagination on Firebase follows a cursor model, and is based on knowing an anchor node. For example, if you've first shown the 100 most recent messages, you know the timestamp and key of the oldest message you've shown. You can then get the 100 messages before that with:

firebase.database().ref("Humidity")
  .orderByChild("time")
  .endAt(oldestTimeValue, oldestKey) // 👈 new
  .limitToLast(100)
  .on("child_added",snapshot => {

This:

  • orders the nodes by their time value.
  • finds the node whose time is oldestTimeValue and whose key (typically a push key like -M...) is oldestKey (just in case there are multiple nodes with the same time value).
  • then takes the 100 nodes before that one.

Upvotes: 1

Related Questions