Firebase Javascript:按降序分页帖子
我的Firebase数据库存储了我想向用户显示的帖子列表。我有一个分页,每个页面存储10篇文章。然而,在它的当前形式中,数据库将从最早的帖子分页到最新的帖子,而不是在这里显示最新的帖子。这是特别有问题的,因为Firebase不允许同时进行多个查询,我需要作为分页系统的一部分来查询密钥,因为每次用户转到下一页时,我都需要存储页面的最后一个密钥,因为新页面上的所有帖子都将从上一页的最后一个密钥开始。如何确保帖子按降序(从最新到最早)列出 Firebase/JavascriptFirebase Javascript:按降序分页帖子,javascript,html,firebase,pagination,Javascript,Html,Firebase,Pagination,我的Firebase数据库存储了我想向用户显示的帖子列表。我有一个分页,每个页面存储10篇文章。然而,在它的当前形式中,数据库将从最早的帖子分页到最新的帖子,而不是在这里显示最新的帖子。这是特别有问题的,因为Firebase不允许同时进行多个查询,我需要作为分页系统的一部分来查询密钥,因为每次用户转到下一页时,我都需要存储页面的最后一个密钥,因为新页面上的所有帖子都将从上一页的最后一个密钥开始。如何确保帖子按降序(从最新到最早)列出 Firebase/Javascript var databas
var database = firebase.database().ref();
var postsRef = database.child("posts");
var currentPage = 0;
var keyArray = [];
var limit = 0;
function listThoughts(page) {
if (page == 0) {
postsRef.orderByChild("key").limitToFirst(10).on("child_added", function(snapshot) {
var thought = snapshot.val();
$("#feed").append("<div class='thought2'><div class='heading'>Posted on " + thought.utctime + " by " + thought.username
+ " | <a href='#' class='reply'>Reply</a></div><div class='else'>" + thought.title + "</div></div");
keyArray[0] = thought.key;
});
}
else {
var elementCount = 0;
console.log("Staring At: " + keyArray[page - 1]);
postsRef.orderByChild("key").limitToFirst(10).startAt(keyArray[page - 1]).on("child_added", function(snapshot) {
var thought = snapshot.val();
console.log("THOUGHT: " + thought);
$("#feed").append("<div class='thought2'><div class='heading'>Posted on " + thought.utctime + " by " + thought.username
+ " | <a href='#' class='reply'>Reply</a></div><div class='else'>" + thought.title + "</div></div");
keyArray[page] = thought.key;
elementCount += 1;
});
if (elementCount < 10) {
limit = page;
}
}
}
listThoughts(currentPage);
function changeTab(direction) {
if (direction == -1 && currentPage > 0) {
currentPage -= 1;
console.log("Current Tab: " + currentPage);
$("#feed").empty();
listThoughts(currentPage);
}
if (direction == 1) {
if (limit == 0 || currentPage < limit) {
currentPage += 1;
console.log("Current Tab: " + currentPage);
$("#feed").empty();
listThoughts(currentPage);
}
}
}
var-database=firebase.database().ref();
var postsRef=database.child(“posts”);
var-currentPage=0;
var-keyArray=[];
var限值=0;
功能列表(第页){
如果(第==0页){
postsRef.orderByChild(“key”).limitToFirst(10).on(“添加child_”),函数(快照){
var think=snapshot.val();
$(“#提要”).append(“+think.username”在“+think.utctime+”上发布
+“|”+think.title+"我的解决方案是将日期存储为每个帖子的键。这样,我既可以按从最新到最旧的顺序同时查询所有帖子,又可以跟踪页面的起始帖子。我的解决方案是将日期存储为每个帖子的键。这样,我可以同时查询所有帖子按从最新到最旧的顺序发布,同时能够跟踪页面的起始帖子
<div class="content-three">
<div id="feed">
</div>
</div>
<div class="center">
<button onclick="changeTab(-1)"><</button>
<button onclick="changeTab(1)">></button>
</div>