0

Below is the code from my front end on selecting players:

$scope.total = 50000000;

 var ref = firebase.database().ref("players");

var ref3 = firebase.database().ref("users").child(uid).child("total");

$scope.players = $firebaseArray(ref);

console.log ($scope.players); 

$scope.picked = [];
$scope.history = [];

$scope.buy = function(player) {
    //remove if already added locally
    var index = $scope.history.indexOf(player);

    if(index>=0){
        $scope.history.splice(index,1);
        return;
    }
    var index2 = $scope.picked.indexOf(player.id);
     if(index2>=0){
         $scope.picked.splice(index2,1);
         return;
     }
    //max 6 allowed
    if($scope.history.length>=6){
        alert('max 6 allowed');
        return;
    }
    if($scope.picked.length>=6){
        alert ('max 6 players');
        return;
    }
    var selected = $scope.history.reduce(function(a,b){
        a[b.position] = (a[b.position] || 0) + 1;
        return a;
    }, {}) || {};

    if(!selected[player.position] || selected[player.position]<2){
        $scope.history.push(player);
        $scope.picked.push(player.id);

    }else{
        alert('You can add only two players per position');

    }
  };

  $scope.getTotal = function(){
    return $scope.history.reduce(function(tot, p){
        tot = tot - p.price;
        return tot;
    }, $scope.total);
  };     


  $scope.saveTeam = function(){

   ref3.set( $scope.picked);       
   $location.path('/mySquad');
   };

This is the database node that is reflected in the scope by the code above:

{
 "players" : [ {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 1,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Yasin 'YB' Amusan",
"position" : "forward",
"price" : 8000000
}, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 2,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Hassan 'Hasi' Akinyera",
"position" : "defender",
"price" : 5000000
}, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 3,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Femi 'Fabio' Awoniyi",
"position" : "defender",
"price" : 9000000
 }, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 4,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Deji 'Dej' Awoniyi",
"position" : "forward",
"price" : 7000000
 }, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 5,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Koye 'K10' Kekere-Ekun",
"position" : "midfielder",
"price" : 9000000
 }, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 6,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Teni 'Teezee' Zacchaeus",
"position" : "hybrid",
"price" : 6000000
 }, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 7,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Bolaji 'Boj' Odojukan",
"position" : "forward",
"price" : 7000000
  }, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 8,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Ernest",
"position" : "defender",
"price" : 6000000
}, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 9,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Samad 'States' Animashaun",
"position" : "midfielder",
"price" : 7000000
  }, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 10,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Zachy Mbadiwe",
"position" : "Forward",
"price" : 10000000
}, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 11,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Laolu 'cutie'",
"position" : "defender",
"price" : 7000000
}, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 12,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Fikayo 'Kyo' Etti",
"position" : "defender",
"price" : 8000000
}, {
"R" : 0,
"Team" : "Industry",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 13,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Efe Tunde-Imoyo",
"position" : "defender",
"price" : 5000000
}, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 14,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Temi 'Forbes' Afolabi",
"position" : "forward",
"price" : 14000000
 }, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 15,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Dami Etomi",
"position" : "defender",
"price" : 7000000
 }, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 16,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Tomi 'Belg' Belgore",
"position" : "defender",
"price" : 8000000
 }, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 17,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Tobi 'Staxx' Kasali",
"position" : "Hybrid",
"price" : 9000000
}, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 18,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Bobo",
"position" : "Hybrid",
"price" : 9000000
 }, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 19,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Ola Adeyemi",
"position" : "defender",
"price" : 5000000
 }, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 20,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Pekun 'Pyzzle' Odutola",
"position" : "Hybrid",
"price" : 9000000
 }, {
"R" : 0,
"Team" : "VGC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 21,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Oreva 'Revz' Amata",
"position" : "forward",
"price" : 11000000
}, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 22,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Tenny 'TK' Karim",
"position" : "Forward",
"price" : 5000000
}, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 23,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Andy Inegbese",
"position" : "Midfielder",
"price" : 5000000
 }, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 24,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Donald 'DO' Ofik",
"position" : "defender",
"price" : 7000000
}, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 25,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Emeke 'Mexxo'",
"position" : "midfielder",
"price" : 6000000
}, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 26,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Ife 'I-Baxx Bakare",
"position" : "midfielder",
"price" : 6000000
}, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 27,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Gabriel 'Gabby' Inegbese",
"position" : "Hybrid",
"price" : 8000000
 }, {
"R" : 0,
"Team" : "YMFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 28,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Jimi Oyelola",
"position" : "Forward",
"price" : 5000000
 }, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 29,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Chad 'Chadea' Oyefolu",
"position" : "Forward",
"price" : 5000000
}, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 30,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Ekoh 'Starboy' Sagoe",
"position" : "midfielder",
"price" : 15000000
}, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 31,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Tomiwa 'JBET' Jaiyeola",
"position" : "Forward",
"price" : 13000000
}, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 32,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Lemar Chris",
"position" : "defender",
"price" : 9000000
}, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 33,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Olumide 'Olic' Williams",
"position" : "defender",
"price" : 8000000
}, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 34,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Leke 'LVD' Dokomu",
"position" : "midfielder",
"price" : 7000000
}, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 35,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Dola Awosika",
"position" : "defender",
"price" : 5000000
 }, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 36,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Nehemiah 'Memphis'",
"position" : "midfielder",
"price" : 7000000
 }, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 37,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Kola 'Skippo' Ayanwale",
"position" : "midfielder",
"price" : 10000000
 }, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 38,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Milekan 'Mileks'",
"position" : "defender",
"price" : 7000000
 }, {
"R" : 0,
"Team" : "ChadFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 39,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Lanre 'Lavigz' Vigo",
"position" : "hybrid",
"price" : 8000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 40,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Alfred 'Flyz' Obande",
"position" : "defender",
"price" : 6000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 41,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Chuka 'Zingy' Azinge",
"position" : "hybrid",
"price" : 6000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 42,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Osobase 'Oso' Omokhodion",
"position" : "midfielder",
"price" : 10000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 43,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Arnold Okuguni",
"position" : "defender",
"price" : 7000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 44,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Ikenna Mbadiwe",
"position" : "defender",
"price" : 6000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 45,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Moukhtar",
"position" : "forward",
"price" : 8000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 46,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Chinedu 'Anuxx' Anukwem",
"position" : "forward",
"price" : 8000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 47,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Tosan 'Mac' Wiltshere",
"position" : "defender",
"price" : 6000000
 }, {
"R" : 0,
"Team" : "FBGFC",
"Y" : 0,
"assists" : 0,
"goals" : 0,
"id" : 48,
"image" : "http://res.cloudinary.com/deji/image/upload/v1489787662/blank_photo_mqvivv.png",
"name" : "Martin Agbaso",
"position" : "forward",
"price" : 5000000
 } ],
"users" : {
"5UBG3yqWnRXe05bByUeASYVbY0l1" : {
  "email" : "john@gamail.com",
  "fullname" : "123456",
  "teamname" : "123456",
  "total" : 0,
  "userName" : "john@gamail.com",
  "week" : 0
},
"AotYBSfTDBhl7vbeWK7WFUh9s3i1" : {
  "email" : "vanessa@yahoo.com",
  "fullname" : "123456",
  "teamname" : "123456",
  "total" : [ 1, 2, 3, 4, 5, 6 ],
  "userName" : "vanessa@yahoo.com",
  "week" : 0
},
"l3J1TVsFNLMi0Oxg6hz4AJpacE53" : {
  "email" : "awoniyideji@yahoo.com",
  "fullname" : "Djflex11",
  "teamname" : "deji awoniyi",
  "total" : [ 1, 1, 5 ],
  "userName" : "awoniyideji@yahoo.com",
  "week" : "no"
}
   }
   }

Each of those numbers represent the id of a player which i have indexed in my database security rules so that each selection by the client's 6 man selection is reflected in the "total" array in the database and then contained in the "$scope.selected" array in the scope.

My issue

My intention is then to iterate through each client's selection and run that iteration through my query below :

    for(let i = 0; i<$scope.history.length;i++){
    return i  
    }
var ref = 
firebase.database().ref("players").orderByChild("id").equalTo(i);
$scope.players = $firebaseArray(ref);

The aim is that the part in the brackets 'insert id number here' is to reflect a a player id.

The code runs perfectly when i manually insert a number directly in the code. However, each client will be selecting 6 numbers randomly. Is there a way to iterate through the selection array so that the respective player ids are reflected at the end of my query each time?

The aim is that the corresponding 6 man team of players will be selected by their ids.

  • Can you replace the pseudo data structure in your question with the actual JSON of your app? You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Sep 06 '17 at 02:17
  • Have you tried using `.forEach` to iterate through the firebase list? Also what is `$firebaseArray`? – sketchthat Sep 06 '17 at 03:52
  • @FrankvanPuffelen i have edited the question to include the 47 players from the database. – Deji Caleb Awoniyi Sep 06 '17 at 09:03
  • @sketchthat $firebaseArray is a method that retruns the data as an array – Deji Caleb Awoniyi Sep 06 '17 at 13:20
  • From re-reading your question it sounds like you're looking for the equivalent of `SELECT * FROM players WHERE ID in [4, 8, 15, 16, 23, 42]`. The Firebase Database doesn't support such queries. See my answer here: https://stackoverflow.com/questions/29560088/firebase-equivalent-to-sql-where-in and here on why it's often not needed https://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Sep 06 '17 at 14:33
  • @FrankvanPuffelen thanks Frank. I've edited the question to include my full code. I haven't been able to get a code that loops through the array in such a way that that the "i" reflects the 6 numbers in the array. – Deji Caleb Awoniyi Sep 06 '17 at 15:55

0 Answers0