Deji James
Deji James

Reputation: 437

How to post an array successfully to Firebase Database

Below is a snapshot of my JSON of Players:

enter image description here

Below is the code used to select the players on the client side:

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

 $scope.buy = function(player) {
        //remove if already added locally
        var index = $scope.history.indexOf(player);
        var index2 = $scope.picked.indexOf(player.id);
        if(index>=0 || index2>=0 ){
            $scope.history.splice(index,1);
            $scope.picked.splice(index2,1);
            return;
        }
 
        //max 11 allowed
        if($scope.history.length>=10 || $scope.picked.length>=10){
            alert('max 10 players allowed');
            return;
        }
        //to make sure moeny doesn't go below $10,000,000
        if($scope.total<0){
            alert('Oops! You have run out of cash');
            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]<4 && 
            $scope.total>0){
            $scope.history.push(player);
            $scope.picked.push(player.id);
               
        }else{
            alert('You can add only four players per position');
             
        }
      };

The code produces the below selected players (as an example):

[
0: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 1, …}
1: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 2, …}
2: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 3, …}
3: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 4, …}
4: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 5, …}
5: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 6, …}
6: {Team: "AC Izzue", assists: 0, cost: "5m", goals: 0, id: 7, …}
7: {Team: "After Hours FC", assists: 0, cost: "5m", goals: 0, id: 16, …}
8: {Team: "Alaye FC", assists: 0, cost: "5m", goals: 0, id: 27, …}
9: {Team: "Alaye FC", assists: 0, cost: "5m", goals: 0, id 26}
]

Each time i try to push or set this array of selected players into my firebase database i get the error below :

Error: Reference.push failed: first argument contains an invalid key ($id) in property 'users.UhIBZDBaLcTcesdRiJ9u7xYv9bd2.picked.0'. Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"

Is it possible to post the array at a user nade on the firebase successfully or does the array have to change?

Upvotes: 0

Views: 83

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83068

It's not easy to understand the code of your question, since it is dependent of several other variables and values.

If you want to get, in your Firebase Realtime Database, what you show in the screenshot attached to your question, you need to pass an Array of objects to the set() method, as follows:

  firebase
    .database()
    .ref('players')
    .set([
      { Team: 'AC Izzue', assists: 0, cost: '5m', goals: 0, id: 1 },
      { Team: 'AC Izzue', assists: 0, cost: '5m', goals: 0, id: 2 },
    ]);

If you use the push() method, which seems to be the case from the error you are receiving, you will create a new child location with an auto-generated key and end with something like:

   "players" : {
    "-MESdJeoccFv8mc5Qhhd" : [ {  // <=== auto-generated key
      "Team" : "AC Izzue",
      "assists" : 0,
      "cost" : "5m",
      "goals" : 0,
      "id" : 1
    }, {
      "Team" : "AC Izzue",
      "assists" : 0,
      "cost" : "5m",
      "goals" : 0,
      "id" : 2
    } ]
  }

Upvotes: 1

Related Questions