如何在Firebase中获取元素/列表的大小而不用全部获取? [英] How to get size of an element/list in Firebase without get it all?

查看:96
本文介绍了如何在Firebase中获取元素/列表的大小而不用全部获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测量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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆