0

I am listing an array based on the votes (orderByChild('up')) and like to divide them into pages (limitToLast(10*formData.page)). However, limitToLast() only takes one attribute rather then two, so for example if I am the page 10, I would have a huge list of items...

ref.orderByChild('up').limitToLast(10*formData.page).once('value', function(snapshot) {
  var data = [];
  snapshot.forEach(function(child) {
    data.unshift(child.val());
  });
  response.writeHead(200, {'Content-Type': 'application/javascript'});
  response.write(JSON.stringify(data));
  response.end();
}, function (errorObject) {
  console.log('Database reading error: ' + errorObject.code);
  response.writeHead(500, {'Content-Type': 'text/html'});
  response.write('Database reading error: ' + errorObject.code);
  response.end();
});

Of course I can use a function similar to this one:

function reversedChildren(snapshot) {
    var children = [];
    snapshot.forEach(function (child) { children.unshift(child); });
    return children;
}

But this method seems to require getting the entire list first and then use snapshot.forEach to reverse the list before turning them into a list. Let's say if the list is 4GB, it means it will need to load 4GB of data to just show 10 entries...

Aero Wang
  • 8,382
  • 14
  • 63
  • 99

0 Answers0