【问题标题】:How can we paginate in Firebase?我们如何在 Firebase 中进行分页?
【发布时间】:2022-01-25 21:05:52
【问题描述】:

从 Google Firebase 列出或获取数据列表时,我们如何分页收集的数据? 例如,

countries = [ 
  {name: 'Afghanistan', code: 'AF'}, 
  {name: 'Åland Islands', code: 'AX'}, 
  {name: 'Albania', code: 'AL'}, 
... 
] 

我想每页列出 10 个,如果我想获得 page =0 with size 10page=5 with size=5

【问题讨论】:

  • 这个问题已经讨论了不少了。你读过questions other asked 并尝试过任何东西吗?如果没有,我建议您先这样做,如果遇到困难,请报告更多详细信息。
  • 它没有涵盖我的问题...有什么解决办法吗?
  • ok 获取前 10 个项目,然后使用第 10 个项目 ID 作为第一个,然后再次获取净 10 个项目。!

标签: firebase pagination


【解决方案1】:

举个例子

{
  -KBZIPRqYmrRgNZ3GJt6: { asc: 1, desc: 9, name: "Rusty Kovacek"},
  -KBZIPRvieZbW-k9R9ra: { asc: 2, desc: 8, name: "Lloyd Feil" },
  -KBZIPRvieZbW-k9R9rc: { asc: 3, desc: 7, name: "Jasmin Hilll" },
  -KBZIPRwiXUgOtv3fCAL: { asc: 4,  desc: 6, name: "Ms. Ibrahim Schinner" },
  -KBZIPRwiXUgOtv3fCAN: { asc: 5, desc: 5,  name: "Dorothea Koepp" },
  -KBZIPRxpCAUyo5TJmY3: { asc: 6, desc: 4, name: "Melvin Marquardt" }, 
  -KBZIPRxpCAUyo5TJmY5: { asc: 7,  desc: 3, name: "Celestine Bode"  },
  -KBZIPRy5Uvz9wUOa6Jx: { asc: 8,  desc: 2, name: "Emerald Olson"  }, 
  -KBZIPRy5Uvz9wUOa6Jz: { asc: 9, desc: 1, name: "Miss Joey Jacobi" }, 
  -KBZIPRzRhuguDLLftQR: { asc: 10, desc: 0, name: "Ms. Denis Rutherford" }
}

var axios = require('axios');
var Firebase = require('firebase');
var namesRef = new Firebase('https://demos-firebase.firebaseio.com/dataDemo/names');

axios.get(namesRef.toString() + '.json?shallow=true')
  .then(function (res) {
    // This list is not sorted!!!
    // res.data = {
    //   '-KBZIPRqYmrRgNZ3GJt6': true,
    //   '-KBZIPRwiXUgOtv3fCAN': true,
    //   '-KBZIPRy5Uvz9wUOa6Jx': true,
    //   '-KBZIPRzRhuguDLLftQR': true,
    //   '-KBZIPRxpCAUyo5TJmY5': true,
    //   '-KBZIPRxpCAUyo5TJmY3': true,
    //   '-KBZIPRwiXUgOtv3fCAL': true,
    //   '-KBZIPRvieZbW-k9R9ra': true,
    //   '-KBZIPRvieZbW-k9R9rc': true,
    //   '-KBZIPRy5Uvz9wUOa6Jz': true
    // }
    var keys = Object.keys(res.data).sort(); // Notice the .sort()!
    var pageLength = 2;
    var pageCount = keys.length / pageLength;
    var currentPage = 1;
    var promises = [];
    var nextKey;
    var query;

    for (var i = 0; i < pageCount; i++) {
      key = keys[i * pageLength];
      console.log('key', key);
      query = namesRef.orderByKey().limitToFirst(pageLength).startAt(key);
      promises.push(query.once('value'));
    }

    Promise.all(promises)
      .then(function (snaps) {
        var pages = [];
        snaps.forEach(function (snap) {
          pages.push(snap.val());
        });
        console.log('pages', pages);
        process.exit();
        // pages = [{
        //   '-KBZIPRqYmrRgNZ3GJt6': {
        //     asc: 1,
        //     desc: 9,
        //     name: 'Rusty Kovacek'
        //   },
        //   '-KBZIPRvieZbW-k9R9ra': {
        //     asc: 2,
        //     desc: 8,
        //     name: 'Lloyd Feil'
        //   }
        // }, {
        //   '-KBZIPRvieZbW-k9R9rc': {
        //     asc: 3,
        //     desc: 7,
        //     name: 'Jasmin Hilll'
        //   },
        //   '-KBZIPRwiXUgOtv3fCAL': {
        //     asc: 4,
        //     desc: 6,
        //     name: 'Ms. Ibrahim Schinner'
        //   }
        // }, {
        //   '-KBZIPRwiXUgOtv3fCAN': {
        //     asc: 5,
        //     desc: 5,
        //     name: 'Dorothea Koepp'
        //   },
        //   '-KBZIPRxpCAUyo5TJmY3': {
        //     asc: 6,
        //     desc: 4,
        //     name: 'Melvin Marquardt'
        //   }
        // }, {
        //   '-KBZIPRxpCAUyo5TJmY5': {
        //     asc: 7,
        //     desc: 3,
        //     name: 'Celestine Bode'
        //   },
        //   '-KBZIPRy5Uvz9wUOa6Jx': {
        //     asc: 8,
        //     desc: 2,
        //     name: 'Emerald Olson'
        //   }
        // }, {
        //   '-KBZIPRy5Uvz9wUOa6Jz': {
        //     asc: 9,
        //     desc: 1,
        //     name: 'Miss Joey Jacobi'
        //   },
        //   '-KBZIPRzRhuguDLLftQR': {
        //     asc: 10,
        //     desc: 0,
        //     name: 'Ms. Denis Rutherford'
        //   }
        // }]
        
      });
  });

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2020-08-28
    相关资源
    最近更新 更多