如何更新 Firebase 对象内的对象映射?

How do I update an Object Map inside a Firebase Object?

这里是新手!

所以我正在使用 Firebase。我基本上有这个压缩结构。我通过尝试遵循我所阅读的内容(无数组等)来获得这种结构。

我有一个需要包含内容列表的用户对象。所以我最终在数据库中得到了这种东西。

{
   name: "clark"
   friends: {
       billyTheKid: true,
       butcherOfBakersfield: true
   }
}

加好友的时候我是这样做的(再次见谅起名不当)

this.$firebaseUserObserver.update({
  friends: {
    [friendID]: true
  }
});

当我这样做时,我 return 我的示例,然后 BillyTheKid 被删除并替换为 ButcherOfBakersfield

尽管使用 update,但我感觉我得到了 set 的行为,所以我一定是错误地处理了结构。

谁能给我指导?

在 ref 上调用 update(),遍历您传入的对象的属性,然后为每个属性调用 set()。所以你确实在每次调用时都替换了 friends

正确的解决方案是在树中的正确级别上调用 update()

this.$firebaseUserObserver.child('friends').update({
    [friendID]: true
});

但在这种情况下甚至不需要,您可以为特定朋友设置呼叫set()

this.$firebaseUserObserver.child('friends').child(friendId).set(true);

如果在 .update 之前下降一级会怎样?前任。

$firebaseUserObserver.$ref.child("friends").update({ [friendId]: true})

我认为更新语句基本上替换了节点的每个 child,而不是 .set 所做的替换整个节点。基本上它在每个 child 上做一个集合。因此,它只能在结构中向下一层工作。