Firebase中的记录总数(我何时计算完?) [英] Total number of records in Firebase (when am I done counting?)
问题描述
然而,我卡住了甚至使用.on('value',...)手动运行来获取计数:
$ $ $ $ $ $ $ $ $ var table =新的Firebase('http://beta.firebase.com/user/tablename');
var count = 0;
table.on('child_added',function(snapshot){
count ++;
//我怎么知道这是否是最后一个孩子?即计数完成?
});
//何时可以使用count?
我预见到任何类型的分页都会出现同样的问题,而且我觉得自己有点疯狂对这个。我错过了什么?
这从根本上说是错误的模式,例如获取用户在他/她的队列中的消息数量? $ b
使用child_added,您无法知道何时收到最后一个项目。如果你想要计算在特定时间点所有的孩子,我建议使用价值事件如下:
pre > var table = new Firebase('http://beta.firebase.com/user/tablename');
$ b table.on('value',function(snapshot){
var count = 0;
snapshot.forEach(function(){
count ++;
});
// count现在可以安全的使用。
});
Counting records in a table is obviously a manual effort until you guys get some of that spiffy new functionality already in the works ;)
However, I'm stuck on even using a manual run with .on('value', ...) to fetch a count:
var table = new Firebase('http://beta.firebase.com/user/tablename');
var count = 0;
table.on('child_added', function(snapshot) {
count++;
// how do I know if this is the last child? i.e. the count is complete?
});
// when is it okay to use count?
I foresee the same issues with any sort of pagination and I feel like I'm being a bit blockheaded about this. What am I missing?
Is this fundamentally the wrong pattern for, say, getting the number of messages a user has in his/her queue?
With "child_added" there is no way to know when you've received the "last" item. If you're looking to count all of the children that existed at a particular point in time, I'd suggest using the "value" event as follows:
var table = new Firebase('http://beta.firebase.com/user/tablename');
table.on('value', function(snapshot) {
var count = 0;
snapshot.forEach(function() {
count++;
});
//count is now safe to use.
});
这篇关于Firebase中的记录总数(我何时计算完?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!