Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么update()在发送多个对象时删除firebase中以前的子对象数据?_Javascript_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript 为什么update()在发送多个对象时删除firebase中以前的子对象数据?

Javascript 为什么update()在发送多个对象时删除firebase中以前的子对象数据?,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我的firebase结构有一个名为“playerNames”的子级。当我像这样发送数据时: firebase.database().ref("games/" + gameId + "/playerNames/").update({[playerName]: 1}); var player = {authId: uid, joinTime: firebase.database.ServerValue.TIMESTAMP, leaveTime: "", name: playerName, stat

我的firebase结构有一个名为“playerNames”的子级。当我像这样发送数据时:

firebase.database().ref("games/" + gameId + "/playerNames/").update({[playerName]: 1});
var player = {authId: uid, joinTime: firebase.database.ServerValue.TIMESTAMP, leaveTime: "", name: playerName, status: "1", ticket1: "", ticket2: "", ticket3: "", totalTickets: totalTickets};

var data = {};
data["games/" + gameId + "/players/" + playerId] = player;
data["games/" + gameId + "/playerNames"] = {[playerName]: 1};

firebase.database().ref().update(data);
玩家名称将与值1一起追加

但当我像这样发送数据时:

firebase.database().ref("games/" + gameId + "/playerNames/").update({[playerName]: 1});
var player = {authId: uid, joinTime: firebase.database.ServerValue.TIMESTAMP, leaveTime: "", name: playerName, status: "1", ticket1: "", ticket2: "", ticket3: "", totalTickets: totalTickets};

var data = {};
data["games/" + gameId + "/players/" + playerId] = player;
data["games/" + gameId + "/playerNames"] = {[playerName]: 1};

firebase.database().ref().update(data);
播放名称中先前的数据将被覆盖

为什么会这样。我做错什么了吗?这是因为

在第一种情况下,将
{[playerName]:1}
对象传递给该方法,并将更新应用于
的“games/”+gameId+“/playerNames/”
节点

在第二种情况下,传递整个
数据
对象,并将更新应用于数据库的根节点(因此整个
“games/”+gameId+“/playerNames/”
节点被替换)

您需要执行以下操作:

var player = {authId: uid, joinTime: firebase.database.ServerValue.TIMESTAMP, leaveTime: "", name: playerName, status: "1", ticket1: "", ticket2: "", ticket3: "", totalTickets: totalTickets};

var data = {};
data["games/" + gameId + "/players/" + playerId] = player;
data["games/" + gameId + "/playerNames/" + playerName] = 1;

firebase.database().ref().update(data);

它将让“游戏/”+gameId+“/playerNames/”下的所有其他节点“不受影响”

世界需要像你这样的天才。谢谢你,先生,这很有效。