0
votes

I'm running into trouble saving and retrieving values in Firebase using Angularfire v2 (http://angularfire.com/documentationv2.html , v2 is necessary because v1 seemed unable to order items by priority)

It's a simple vote-up-down app using Persona Login for authorization, the problem comes when the user votes on a 'story', I want to add his user id to 'story/users', but it seems the only way to do that without erasing all the other values is to use '$add'(push), which throws in a random key name which I have no idea how to query against.

So to prevent double voting I have to somehow ask if 'story/users' already has that user's id in there.

story---
        users---
                -J9LrtJwXnCI3ZYqsIz1: user-1
                -J9Lriauhfdoaiuhfafd: user-2

Does anybody have any idea how to find out if user-1 is in 'story/users'?

Keep in mind Angularfire v2 has some major changes.

And if anybody has a better idea I'd be happy to hear it (but I'm really trying to avoid saving it as user-1/story-id, for other reasons)

1

1 Answers

1
votes

Store the records by the user's ID, rather than a random push id. For example, you could create a votes path, and keep track of who has voted there:

/votes/user-1
/votes/user-2

Then you can write a security rule to prevent double voting as follows:

{
   "rules": {
       "votes": {
            "$record_id":  {
                // something like this
                ".write":  "auth.id === $record_id && data.val() === true"
            }
       }
   }
}

You can perform the set in V2 angularFire by using the $set and $child commands.

dataRef.$child( user.id ).set( true ); // for example