gpt4 book ai didi

javascript - 如何对包含 equalTo() api 的 firebase 查询进行分页

转载 作者:行者123 更新时间:2023-12-04 00:46:38 25 4
gpt4 key购买 nike

我正在使用 Firebase 编写一个购物网站。我在边栏中有一些类别和子类别,单击它们时,帖子提要应相应更新,按时间排序并仅显示所选类别的帖子。

在 firebase 数据库上发布对象:

    -KiKGQUZ4XaZPHxvOZD-addclose {
categorie:0
subCat: 2
....
}

有很多帖子,我需要对它们进行分页

要做到这一点,我应该这样查询:

  dbRef
.orderByChild('categories')
.equalTo(category)
.limitToLast(QUERY.PAGINATION_COUNT)
.on('value', (snapshot) => {
cb(_toArray(snapshot).reverse());
});

但我不能使用 equalTo() 进行分页。

我认为解决方案应该是:

  dbRef
.orderByChild('categories')
.equalTo(category)
.startAt(lastFetchedItem.fbKey)
.limitToLast(QUERY.PAGINATION_COUNT)
.on('value', (snapshot) => {
cb(_toArray(snapshot).reverse());
});

但是使用 firebase 不可能同时执行 equalTo 查询和 startAt

我该如何解决?

最佳答案

基于 this source看起来这个功能是不可能的,也不会被添加。不过你可以做这样的事情

  1. 使用equalTo查询你的初始分页数量
  2. 按以最后一个可见键结尾的键查​​询,如果类别与您想要的匹配,则过滤结果然后添加它,否则忽略它。

我用 Swift3 编写代码,但遇到了同样的问题,也许我的代码对其他人有帮助。

static func filterTableByCategory(tableNumber: String, category: String) {
filteringRef.child(tableNumber).queryOrdered(byChild: "category").queryEqual(toValue: category).queryLimited(toLast: 9).observeSingleEvent(of: .value, with: { snap in

if snap.exists() {
currentTablePosts.removeAll()
for child in snap.children {
let child = child as? DataSnapshot
if let post = child?.value as? [String: AnyObject] {
let posst = Post()
if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let category = post["category"] as? String, let table = post["group"] as? String, let userID = post["userID"] as? String, let numberOfComments = post["numberOfComments"] as? Int, let region = post["region"] as? String {

posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
posst.fancyPostDescription = Helper.createAttributedString(author: author, postText: postDescription)
posst.postDescription = author + ": " + postDescription
posst.timestamp = timestamp
posst.table = table
posst.region = region
posst.category = category
posst.numberOfComments = numberOfComments
posst.userWhoPostedLabel = Helper.createAttributedPostLabel(username: author, table: table, region: region, category: category)

if let people = post["peopleWhoLike"] as? [String: AnyObject] {
for(_, person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
currentTablePosts.insert(posst, at: 0)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableCollectionView"), object: nil)
} // end of if let
}
}
}
else {
currentTablePosts.removeAll()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableCollectionView"), object: nil)
}
})
filteringRef.removeAllObservers()
}

static func getMoreFilterByCategoryPosts(tableNumber: String, category: String, lastVisibleKey: String) {

pagingReference.child(tableNumber).queryOrderedByKey().queryEnding(atValue: lastVisibleKey).queryLimited(toLast: 12).observeSingleEvent(of: .value, with: { snap in

if snap.exists() {
let currentNumberOfPosts = currentTablePosts.count
for child in snap.children {
let child = child as? DataSnapshot
if let post = child?.value as? [String: AnyObject] {
if let cat = post["category"] as? String, let postID = post["postID"] as? String {
if cat == category && postID != lastVisibleKey {
let posst = Post()
if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let table = post["group"] as? String, let userID = post["userID"] as? String, let numberOfComments = post["numberOfComments"] as? Int, let region = post["region"] as? String {

posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
posst.fancyPostDescription = Helper.createAttributedString(author: author, postText: postDescription)
posst.postDescription = author + ": " + postDescription
posst.timestamp = timestamp
posst.table = table
posst.region = region
posst.category = cat
posst.numberOfComments = numberOfComments
posst.userWhoPostedLabel = Helper.createAttributedPostLabel(username: author, table: table, region: region, category: category)

if let people = post["peopleWhoLike"] as? [String: AnyObject] {
for(_, person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
currentTablePosts.insert(posst, at: currentNumberOfPosts)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableCollectionView"), object: nil)
} // end of if let
}
}
}
}
}
else {
print("snap == nil")
}

})
pagingReference.removeAllObservers()

}

关于javascript - 如何对包含 equalTo() api 的 firebase 查询进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com