Firebase总用户数 [英] Firebase total user count

查看:220
本文介绍了Firebase总用户数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让所有的用户在火力点数? (通过密码,脸书,微博等认证)所有社交和电子邮件密码认证用户。

解决方案

没有内置的方法可以获取用户总数。

您可以保留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屋!

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