Firebase实时数据库分页

Firebase实时数据库分页

问题描述:

我正在根据品牌过滤产品.我可以获取最后一项的ID,并在点击加载"按钮后使用它来获取另一个列表.但是,当某个特定品牌的产品用尽时,它也会继续加载其他品牌的产品.

I am filtering products based on brand. I am able to get the id of the last item and use it to get another list upon clicking on load button. However when products from the particular brand are exhausted it keeps loading products from other brands too.

firebase.database().ref('products').orderByChild('brand')
            .startAt(brand, lastKey)
            .limitToFirst(31)
            .once("value", function(snapshot){
                var data = snapshot.val()
                ...
})

我为类别设置了另一种分页设置,并且效果很好.在耗尽特定类别的所有产品之后,立即停止.

I have another pagination setup for the categories and it works perfectly. Stops exactly after it exhausts all products from the particular category.

firebase.database().ref('products').orderByChild('category/'+type)
                .startAt(type, lastKey)
                .limitToFirst(31)
                .once("value", function(snapshot){
                    var data = snapshot.val()
                    ...
})

我在思考如何根据品牌订购商品可能存在问题.品牌持有字符串值时,类别持有对象.

I am thinking there might the problem on how I order the items based on brand. While brand holds a string value the category holds object.

您正在使用 orderByChild().startAt()过滤器,这意味着Firebase会根据您指定的属性对结果进行排序,然后从您指定的节点开始返回结果.由于您未指定任何结束条件,因此它将一直返回结果,直到到达列表末尾为止.

You're using a orderByChild().startAt() filter, which means Firebase orders the results by the property you indicate, and then starts returning results from the node you indicate. Since you don't specify any end criteria, it keeps returning results until it reaches the end of the list.

如果只想让其 brand 值与您在 startAt()中指定的值匹配的子节点,则还应包括一个 endAt()条件.

If you want to only have child nodes whose brand value matches the value you specify in startAt(), you should also include an endAt() condition.

类似的东西:

firebase.database().ref('products').orderByChild('brand')
        .startAt(brand, lastKey)
        .endAt(brand+'\uf8ff')
        .limitToFirst(31)

\ uf8ff 只是最后一个可打印的Unicode字符,这意味着列表在该品牌之后停止.

The \uf8ff is just the last printable Unicode character, which means the list stops after that brand.