Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  188] [ 1]  / answers: 1 / hits: 17155  / 9 Years ago, fri, december 4, 2015, 12:00:00

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.


More From » angularjs

 Answers
78

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());
});

[#64160] Wednesday, December 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;