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

时间:2017-04-22 15:22:02

标签: javascript firebase

我正在用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

如何解决?

2 个答案:

答案 0 :(得分:0)

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

  1. 使用equalTo
  2. 查询初始分页量
  3. 按最后一个可见键结束的键查询,如果类别与您想要的类别匹配,则过滤结果,然后将其添加,否则忽略它。
  4. 我在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()
    
        }
    

答案 1 :(得分:0)

我最终改变了firebase实时数据库架构。

我不确定,但我认为firebase故意不释放更多api。它迫使你仔细检查你的架构并改进它。

我为类别创建了一个专用集合,并在类别集合中为每个类别创建了单独的节点。 它是构建数据库的firebase recommend

然后将每个新帖子放入相应的类别。

因此,我可以轻松地在每个类别和子类别中使用OrderByKey(),并使用startAtendAt apis

对其进行分页

另一种解决方案是使用firebase功能和弹性搜索。 为此目的创建了FlashLight package