[英]Firebase Real time database pagination
I am trying to add some pagination functionality to my Express + Firebase Realtime Database SDK but I am not sure how to achieve this, and the documentation is not helping me.我正在尝试向我的 Express + Firebase 实时数据库 SDK 添加一些分页功能,但我不确定如何实现这一点,并且文档对我没有帮助。 Also, all the examples I found are regarding Firestore.
此外,我发现的所有示例都与 Firestore 相关。
Just as an example I have this model User which is accessed by database.ref('users')
.举个例子,我有这个模型User ,它由
database.ref('users')
。 Imagine I have 10 users whose IDs go from 1 to 10 respectively and I want to paginate 5 per page.想象一下,我有 10 个用户,他们的 ID 分别从 1 到 10,我想每页分页 5 个。
What I expect is get users whose keys go from 1 to 5, and then when someone clicks on page 2, it will get users whose keys go from 6 to 10.我期望的是获取密钥从 1 到 5 的用户,然后当有人点击第 2 页时,它将获取密钥从 6 到 10 的用户。
According to the documentation, I understood that I should add something like the following:根据文档,我明白我应该添加如下内容:
(req, res) => {
const { key } = req.query;
let ref = database.ref('users')
.orderByChild('createdAt')
.limitToLast(5);
if (key) {
ref = ref.startAt(key);
}
ref.once('value')
.then(snapshot => ...);
}
What I get until now is that the difference between limitToLast()
and limitToFirst()
is the sorting, it's like ORDER BY createdAt DESC
and ORDER BY createdAt ASC
respectively.到目前为止,我得到的是
limitToLast()
和limitToFirst()
之间的区别在于排序,它分别类似于ORDER BY createdAt DESC
和ORDER BY createdAt ASC
。
If I set ref.startAt(5)
, the previous code doesn't work since I am getting the first five users (1 to 5).如果我设置
ref.startAt(5)
,则前面的代码不起作用,因为我获得了前五个用户(1 到 5)。
What approach should I use?我应该使用什么方法? Thanks in advance.
提前致谢。
I got that if I do database.ref('users').orderByChild('createdAt').limitToLast(5).startAt(5)
I get documents where createdAt
is greater that 5 which is wrong.我说,如果我做
database.ref('users').orderByChild('createdAt').limitToLast(5).startAt(5)
我得到的文件,其中createdAt
是大于5这是不对的。 I should sort by date after getting those documents whose keys are next to 5.我应该在获得那些键在 5 旁边的文档后按日期排序。
I was struggling with a very similar scenario, although in reverse - I wanted to display the last 10 records, and then paginate to the beginning of the list (in my case the list was ordered by date, and I wanted to display the latest dates first);我在一个非常相似的场景中挣扎,尽管相反 - 我想显示最后 10 条记录,然后分页到列表的开头(在我的情况下,列表是按日期排序的,我想显示最新的日期第一的);
For your example however, I was able to paginate from 1-5, then 6-10 by implementing the following:但是,对于您的示例,我可以通过实现以下内容从 1-5 和 6-10 进行分页:
First 5 Users:前 5 位用户:
database
.ref('users')
.orderByChild('createdAt')
.limitToFirst(6) //Note here that the request is for the first 6 results
.once("value")
.then((snap) => {
const firstSix = snap.val();
const sixth = firstSix[5].createdAt; //assuming your data is an array, get the last entry
const startAtNext = sixth, //this is the 6th value used for pulling the next 5 results - this should be stored globally
const currentUserList = firstSix.slice(0, firstSix.length -1), //the list of users 1-5
});
And for the next 5 users:对于接下来的 5 个用户:
database
.ref('users')
.orderByChild('createdAt')
.startAt(startAtNext) // Globally stored variable from first call
.limitToFirst(6) //Note here that the request is for the first 6 results
.once("value")
.then((snap) => {
const nextSix = snap.val();
const sixth = nextSix[5].createdAt; //assuming your data is an array, get the last entry
const startAtNext = sixth, // the start index for the next request
const currentUserList = firstSix.slice(0, firstJobsList.length -1), //the next 5 users
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.