代码之家  ›  专栏  ›  技术社区  ›  Deji James

如何将数组成功发布到Firebase数据库

  •  0
  • Deji James  · 技术社区  · 6 年前

    以下是我的玩家JSON快照:

    enter image description here

    下面是在客户端选择玩家的代码:

    $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');
                 
            }
          };
    

    [
    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}
    ]
    

    每次我试着 push set

    错误:参考.推送失败:第一个参数在属性“”中包含无效的键($id)用户.UhIBZDBaLcTcesdRiJ9u7xYv9bd2.picked.0'。键必须是非空字符串,并且不能包含“.”、“\”、“$”、“/”、“[”或“]”

    是否可以在firebase上的用户地址上成功地发布阵列,或者阵列是否必须更改?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Renaud Tarnec    6 年前

    如果你想在你的Firebase实时数据库中获取你在问题所附截图中显示的内容,你需要将一个对象数组传递给 set()

      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 },
        ]);
    

    push()

       "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
        } ]
      }