如何分页包含equalTo()api的firebase查询 [英] How to paginate firebase query which contain equalTo() api
问题描述
我有一个在侧边栏中的一些类别和子类别,onClick他们,帖子提要应相应更新,按时间排序,只显示选定的类别的帖子。
在firebase数据库中发布对象:
-KiKGQUZ4XaZPHxvOZD-addclose {
分类:0
subCat :2
....
}
有很多帖子和我需要分页
为此,我应该这样查询:
dbRef
.orderByChild('categories')
.equalTo(category)
.limitToLast(QUERY.PAGINATION_COUNT)
.on('value',(snapshot)= > {
cb(_toArray(snapshot).reverse());
});
但是我无法用equalTo()分页。
我认为解决方案应该是:
$ $ $ $ $ $ $ $ $ $ $ $ b .orderByChild('categories')
.equalTo(category)
.startAt(lastFetchedItem.fbKey)
.limitToLast(QUERY.PAGINATION_COUNT)
.on('value', (快照)=> {
cb(_toArray(snapshot).reverse());
});
但是Firebase无法做到 equalTo
查询和 startAt
在同一时间
如何解决它?
我最终更改了firebase实时数据库模式。
我不确定,但我认为firebase有意不会释放更多apis。它强制你仔细检查你的模式,并改进它。
我已经创建了一个专门的类别集合和类别集合为每个类别创建单独的节点。
Firebase推荐来构建您的数据库。
然后把每个新帖子放到相应的类别中。
所以我可以很容易地使用 OrderByKey()
,并使用 startAt
或 endAt
apis
另一个解决方案是使用firebase函数和弹性搜索。
FlashLight软件包是为此目的而创建的。
I'm coding a shopping website with firebase. I have a some categories and subcategory in the sidebar, which onClick them, posts feeds should be updated accordingly, ordered by time and just show posts from selected category.
post object on the firebase database :
-KiKGQUZ4XaZPHxvOZD-addclose {
categorie:0
subCat: 2
....
}
There's a lot of posts and i need to paginate them
To do it i should query like this:
dbRef
.orderByChild('categories')
.equalTo(category)
.limitToLast(QUERY.PAGINATION_COUNT)
.on('value', (snapshot) => {
cb(_toArray(snapshot).reverse());
});
But i can not paginate with equalTo().
I think the solution should be :
dbRef
.orderByChild('categories')
.equalTo(category)
.startAt(lastFetchedItem.fbKey)
.limitToLast(QUERY.PAGINATION_COUNT)
.on('value', (snapshot) => {
cb(_toArray(snapshot).reverse());
});
But its not possible with firebase to do equalTo
query and startAt
in the same time
How to i can solve it?
I ended up changing firebase realtime database schema.
I'm not sure but i think firebase intentionally doesn't release more apis. Its forcing you to double check your schema and improve it.
I have created a dedicated collection for categories and in the categories collection created separate node for each category. Its what firebase recommend to structure your database.
Then put each new post into the corresponding category.
So i can easily use OrderByKey()
in each category and sub category and paginate them using startAt
or endAt
apis
An another solution would be using firebase function and elastic search. FlashLight package has been created for this purpose.
这篇关于如何分页包含equalTo()api的firebase查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!