我们如何在Firebase中进行分页?

时间:2017-09-16 03:24:05

标签: firebase pagination

在列出或从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

1 个答案:

答案 0 :(得分:2)

作为一个例子

{
  -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'
        //   }
        // }]

      });
  });