0

I have a small app where i record votes and reorder a list based on those votes. The app has users and each user can only vote once. For every record, when a user votes, i save the user id.

   var obj = {};
   obj[$scope.user.id] = {
       avatar: $scope.user.avatar,
       name  : $scope.user.name
   };
   myDataRef.child("sessions/"+sessionId+"/list/"+key+"/votes")
                            .update(obj, function(error){}

The update() protects me, and does not allow repeated ids. With this i know that i have as many unique votes as voters. So, i want a field called 'score' that would automatically count the users and set the 'score' field with that count. How can i achieve this?

Tks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    See http://stackoverflow.com/questions/37954217/is-the-way-the-firebase-database-quickstart-handles-counts-secure/37956590#37956590. But be aware that this is non-trivial in a real-time distributed client-only system. – Frank van Puffelen Jul 15 '16 at 19:27
  • doesn't seem like a duplicate. I don't have the answer to my question. its rather an alternative. I'm already doing that. I'm asking for a simple automatic count because it would fit my needs and would be much simpler. – Pedro Afonso Jul 18 '16 at 09:39
  • It would indeed be a lot simpler if Firebase had built-in counting functionality. We're aware of the demand for it, but it doesn't exist at the moment. The question I linked is the current closest approach, unless you're willing to set up a server/run a trusted process. – Frank van Puffelen Jul 18 '16 at 16:29
  • Yes, it would :) I just followed your suggestion. Its working as intended. tks a lot. – Pedro Afonso Jul 19 '16 at 18:21

0 Answers0