如何在Firebase中获取元素/列表的大小而不用全部获取? [英] How to get size of an element/list in Firebase without get it all?
问题描述
我需要测量Firebase中元素或列表的大小/长度,但不需要真实的内容。
类似于 firebase.database()。ref(users /)。once(length||size)。then(callback)
ps Javascript SDK
Firebase没有计数运算符,方法是下载所有的孩子或保持单独的< children> _count
属性同步。后者不是一项简单的任务(参见这里回答一个方法),所以大多数开发人员最终可能会用下载过多的数据而不是微不足道的方法:
(value,function(snapshot)){
console.log(There +snapshot.numChildren() +messages);
})
用 shallow = true
参数来发起一个REST调用,这会给你一个键。请参阅在Firebase中,是否有一种方法可以在不加载所有节点数据的情况下获得一个节点的子节点数?
I need to measure the size/length of an element or list in Firebase but I don't need the real content.
Something like firebase.database().ref("users/").once("length" || "size").then(callback)
ps Javascript SDK
Firebase doesn't have a count operator, so the only way is to download all children or keep a separate <children>_count
property in sync. The latter is not a trivial task (see my answer here for one approach), so most often developers likely end up going with the downloads-too-much-data-but-is-trivial approach:
ref.child("messages").on("value", function(snapshot) {
console.log("There are "+snapshot.numChildren()+" messages");
})
A more efficient way to count the children would be to fire a REST call with shallow=true
parameter, which will give you just the keys. See In Firebase, is there a way to get the number of children of a node without loading all the node data?
这篇关于如何在Firebase中获取元素/列表的大小而不用全部获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!