2017-02-27 85 views
1

SETUP爲什麼在多個位置更新的行爲像SET而不是UPDATE?

/demo/key就像{a:1, b:2, c:3}

一個值。在/user/demo/key有像true

var database = firebase.database(); 
var rootRef = database.ref(); 

方案A

rootRef.child('demo/key').update({a:0}); 
// Result -> {a:0, b:2, c:3} 
// b and c not overwritten, still exist -> happy 
// update() acts like I expected/read 

方案B的值

var data = {a:7}; 
var updates = {}; 
    updates[`/demo/${key}`] = data; 
    updates[`/user/demo/${key}`] = true; 

rootRef.update(updates); 
// Result -> {a:7} 
// b:2, c:3 are gone -> Acts like set() ??? 
// Expected -> {a:7, b:2, c:3} 

我不知道爲什麼這是......但我本來預計update()函數將在任一實例中執行相同的操作並執行部分寫操作。

我做錯了什麼或缺少一個關鍵概念?

回答

2

它確實像update一樣工作。當您將數據傳遞到update時,每個密鑰都是set,並且未包含在數據中的密鑰的兄弟未被觸動。

您需要做的所有事情以確保bc不會被刪除是使用更具體的密鑰。例如:

var updates = {}; 
    updates[`/demo/${key}/a`] = 7; 
    updates[`/user/demo/${key}`] = true; 
+0

嗯。我明白你在說什麼。我想我的想法是,數據屬性會反映爲更具體的鍵。我可以做的是採取'數據= {a:7}'並動態生成更具體的更新引用,以免覆蓋。這是人們做的事嗎? – jmk2142

+1

是的,這就是我會做的。 – cartant

相關問題