Can I get the nth item of a firebase “query”?

前端 未结 2 1385
闹比i
闹比i 2020-12-19 11:19

Firebase has some very basic query functionality with orderBy*, limitTo*, startAt(), etc. Is there a way to tell Firebase you want the

2条回答
  •  情书的邮戳
    2020-12-19 12:01

    While you cannot access child items by index with Firebase, you can store the key of an item and use that to start a 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 a variable lastKnownKey that has the last key you've ever seen. To get the next batch of children, you pass that value in to startAt():

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

提交回复
热议问题