Upon examining my firebase database, I found the following structure:
Users {
askdfasdf: John: {
Selection: [1,2,3]
},
fasfadffe: Mark: {
Selection: [1,2,4]
}
}
Players {
{
name: 'Messi',
agility: 90,
id: 1
},
{
name: 'Beckham',
agility: 54,
id: 2
},
{
name: 'Rooney',
agility: 10,
id: 3
},
{
name: 'Neymar',
agility: 84,
id: 4
}
}
To access the data in the database, I used the following code snippet:
var ref = firebase.database().ref("players");
var ref3 = firebase.database().ref("users").child(uid).child("total");
$scope.players = $firebaseArray(ref);
$scope.selection = $firebaseArray(ref3);
I am currently exploring ways to iterate through two arrays in search of matching values. Specifically, I am looking for a method to loop through the "players" array and identify players whose id
s match the numbers in the "selection" array.
The main objective is to display each client's selections on the page once they have made their choices.
In terms of security rules for the database, here is what I have set up:
{
"rules": {
"players":{
".read" : "auth != null",
".write" : "auth != null",
".indexOn": "id"
}
}
}
I attempted to iterate over each selection with the following code:
$scope.getSelectedPlayers = function (){
for (let i = 0; i<$scope.selection.length; i++){
return $scope.selection[i];
var ref=
firebase.database().ref("players").orderByChild("id").equalTo($scope.selection[i]);
}
Unfortunately, this approach did not yield the desired results