将数据保存到Firebase中的子级

将数据保存到Firebase中的子级,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,我有一个小应用程序,用户可以根据球员最近在各种运动中的表现向上或向下投票 当前单击upvote按钮时,投票者的UID(通过谷歌登录)应该被推送到被投票的相应玩家的数据库中。像这样: 然而,它目前正在这样做: 下面是执行Firebase工作的代码。我相信这是我错的一句话 this.database.child(playerId).transaction 或 ref=firebase.database().ref('玩家/投票者') 守则: this.database = firebase.da

我有一个小应用程序,用户可以根据球员最近在各种运动中的表现向上或向下投票

当前单击upvote按钮时,投票者的UID(通过谷歌登录)应该被推送到被投票的相应玩家的数据库中。像这样:

然而,它目前正在这样做:

下面是执行Firebase工作的代码。我相信这是我错的一句话

this.database.child(playerId).transaction

ref=firebase.database().ref('玩家/投票者')

守则:

this.database = firebase.database().ref().child('players');

upvotePlayer(playerId) {
this.state.user ?
  this.database.child(playerId).transaction(function (player) {
    if (player) {
      console.log("UID: " + uid)
      var ref = firebase.database().ref('players/voters');
      ref.child(uid).set(1);
    }
    return player;
  })
  :
  console.log("Must be logged in to vote.")

  }

问题在于:

 var ref = firebase.database().ref('players/voters');
它将
投票者
添加到
玩家
节点下,而不是
推送id
节点下

因此,您必须从数据库中检索pushid,然后执行以下操作:

  var ref=firebase.database().ref().child('players').child(playerId).child('voters');
因此,选民将成为普希德的孩子之一

面向未来观众:

最好使用事务,因为:

如果多个用户同时对同一帖子进行upvote,或者客户端有过时数据,则使用事务可以防止upvote计数不正确


实际上,您正在使用玩家id将值推送到付款人节点。您已经将数据推送到玩家id的子节点
投票者
。请尝试以下代码:-

this.database = firebase.database().ref().child('players');

upvotePlayer(playerId) {
this.state.user ?
  this.database.child(playerId).transaction(function (player) {
    if (player) {
      console.log("UID: " + uid)
      player.ref.child(voters).push(uid);
    }
    return player;
  })
  console.log("Must be logged in to vote.")

  }
问题是您实际上正在引用
玩家
节点,并在名为
投票者
的playerid的相同级别上创建新节点。在下一行中,只需在与playerid相同的级别上创建一个新节点
投票者

  var ref = firebase.database().ref('players/voters');
  ref.child(uid).set(1);

如您所见,在
玩家
根节点中没有名为
投票者
的节点。但是您正在设置该值,因此它是使用
set()
函数创建的新数据

您正在将投票存储在此路径
玩家/选民
中,而它应该位于此路径
玩家/$playerId/选民

结帐

this.database = firebase.database().ref().child('players');

upvotePlayer(playerId) {
    this.state.user ?
        this.database.child(playerId).transaction(function (player) {
            if (player) {
                console.log("UID: " + uid)
                var ref = firebase.database().ref('players/' + playerId + '/voters');
                ref.child(uid).set(1);
            }
            return player;
        })
        :
        console.log("Must be logged in to vote.")
}

方法有
transactionUpdate
函数作为参数,它应该返回新值,而您没有返回任何值,而且我认为更好的解决方案是不使用transaction

编辑:不带事务的解决方案
好的,playerid是uid还是pushid?对不起,我应该更具体一点。playerId是在firebase-L3vkv中为每位运动员提供的唯一推送ID。。等等,在图像示例中。已登录投票者的UID似乎是正确的UID。问题是,当我单击upvote并查看firebase数据库时,它显示选民出现,但变为红色并消失。由于某些原因,数据没有保留。是否添加此
ref.child(uid).set(1)?显示红色后还有其他数据吗?是的,加上那一行。如果没有该行,数据库中就不会出现任何内容。没有其他数据出现,只有选民。是的,你必须写那一行。试试这个
ref.child(uid).update(1)而不是此
ref.child(uid).set(1)@idlehandError:Reference.update失败:第一个参数必须是包含要替换的子对象的对象。->137 | ref.child(uid).更新(1);'投票者成功地短暂地展示了它应该在哪里,但像其他解决方案一样,它变红并消失在Firebase中。你知道为什么吗?那是因为你使用事务,如果我在你的情况下,我不会使用事务,签出编辑谢谢-我没有完全意识到事务功能的性质。谢谢你的帮助
upvotePlayer(playerId) 
{
    if(this.state.user)
    {
        let ref = firebase.database().ref('players/' + playerId + '/voters');
        ref.child(uid).set(1);
    }
    else
    {
        console.log("Must be logged in to vote.")
    }
}