Is there a way to get a firebase element from a certain index?

久未见 提交于 2019-12-11 15:12:28

问题


I have a list of chat messages that are ordered by timestamp and have a unique firebase key (made by the push operator)

In this list I want to get the message at position 100 from the end without knowing its key. Is there a way to do this?

I tried using the filtering methods limitToLast() and limitToFirst together which created an error, saying a filter was already set.

The current way I do it is by downloading all 100 messages with:

    admin.database().ref("path/to/messages").limitToLast(100)

And then I look into that list for the first key which is the one I want.

Is ther a way that I can get the 100th message without downloading all the other messages as well?


回答1:


Firebase Realtime Database queries don't support specifying an offset, but instead require that you know the key values of the anchor document. If you don't know those, you will have to read all documents before/after the one you search for.

Also see:

  • How to change limitToFirst() dynamically in recyclerview firebase?
  • AngularJS : Fetch records for pagination from Firebase
  • how to read firebase database
  • Firebase paging data. Is it possible?



回答2:


citiesRef.orderBy("population").startAt(1000000)

Source: Paginate data with query cursors



来源:https://stackoverflow.com/questions/57570929/is-there-a-way-to-get-a-firebase-element-from-a-certain-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!