Firebase总用户数 [英] Firebase total user count
问题描述
没有内置的方法可以获取用户总数。
您可以保留userIds的索引并将其拉下来并对其进行计数。然而,这将需要下载所有的数据来计算。
{
userIds:{
user_one:true,
user_two:true,
user_three:true
}
}
然后当下载数据时,您可以调用 snapshot.numChildren()
:
var ref = new Firebase('< my-firebase-app> / userIds');
ref.once('value',function(snap){
console.log(snap.numChildren());
});
如果您不想下载数据,则可以使用交易。
var ref = new Firebase('< my-firebase-app>');
ref.createUser({email:'',password:'',function(){
var userCountRef = ref.child('userCount');
userCountRef.transaction(function(current_value ){
//将用户计数增加一个
return(current_value || 0)+ 1;
});
});
然后您可以实时地收听用户:
var ref = new Firebase('< my-firebase-app> / userCount');
ref.on('value',function(snap){
console.log(snap.val());
});
Is there a way to get all the users' count in firebase? (authenticated via password, facebook, twitter, etc.) Total of all social and email&password authenticated users.
There's no built-in method to do get the total user count.
You can keep an index of userIds and pull them down and count them. However, that would require downloading all of the data to get a count.
{
"userIds": {
"user_one": true,
"user_two": true,
"user_three": true
}
}
Then when downloading the data you can call snapshot.numChildren()
:
var ref = new Firebase('<my-firebase-app>/userIds');
ref.once('value', function(snap) {
console.log(snap.numChildren());
});
If you don't want to download the data, you can maintain a total count using transactions.
var ref = new Firebase('<my-firebase-app>');
ref.createUser({ email: '', password: '', function() {
var userCountRef = ref.child('userCount');
userCountRef.transaction(function (current_value) {
// increment the user count by one
return (current_value || 0) + 1;
});
});
Then you can listen for users in realtime:
var ref = new Firebase('<my-firebase-app>/userCount');
ref.on('value', function(snap) {
console.log(snap.val());
});
这篇关于Firebase总用户数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!