1

I'm making a realtime app with AngularJS and Firebase. I'm storing an array of data (players). Below you can see a sample of my datastructure.

enter image description here

I can get all the players of this particular game, but how do I get for instance the player 'test'? The key is a generated value:

Game.find(gameId).child('/players/').push(player);

Then I thought, why don't I just 'update' '/players/'+name and set the name there. But unfortunatly I'm getting back objects inside an object:

{
   players : {
     "name":"...",
     "test":"moretest"
   }
}

I can access these properties but I cannot loop over them because it's not an array.

So basically what I want to do is "linq-wise" getting a player out of an array.

Game.find(gameId).child('/players/').where( name == ...);

Thanks in advance

Edit:

enter image description here

1 Answer 1

3

You are on the right track with storing just the name of the player instead of using push to generate an ID. You'll need to make sure your player names are unique though, otherwise you risk not being able to accommodate two players with the same name.

Every key in Firebase must have a value, so the best way to store a list of players where you want them to be accessible by the player name is to simply set them to a boolean value like 'true':

Game.find(gameId).child('/players').child(player).set(true);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, see my edited post. As you can see a player contains more than just the name. Basically I need to set the "turn" property to true for the first Player found in the /players collection. But as I said, if I go search for all the players, it doesn't return an array. It returns an object with objects. ng-repeat doesn't work and I don't know how I can set the "turn" property to true for the first player.
You can certainly use ng-repeat with objects. Use ng-repeat="(key, value) in collection".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.