我根据投票 ( orderByChild('up')
) 列出了一个数组,并喜欢将它们分成页面 ( limitToLast(10*formData.page)
)。但是,limitToLast()
只需要一个属性而不是两个,所以例如,如果我是第 10 页,我会有一个巨大的项目列表......
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();
});
当然,我可以使用与此类似的功能:
function reversedChildren(snapshot) {
var children = [];
snapshot.forEach(function (child) { children.unshift(child); });
return children;
}
但是这种方法似乎需要先获取整个列表,然后使用 snapshot.forEach 来反转列表,然后再将它们变成列表。假设列表是 4GB,这意味着它需要加载 4GB 的数据才能显示 10 个条目......