firebase $add() .push() .set()

我是研究僧i 提交于 2019-12-05 10:28:58

If I have the following data tree in Firebase:

{
  users:
   {
      key: { name:"bob" }
   }
}

When I do an $add, I will create a new item in the tree

$scope.data.child('users').$add({
    name: name
  });

Since $add uses the Push method in Firebase, new random Key will be used when pushing data to the child.

{
  users:
   {[
      key: { name:"bob" },
      key2: { name:"name" }
   ]}
}

If I do a set on the same Users object, I will overwrite the data that is already there. So, in your example, without specifying a key, you will overwrite the entire user object.

$scope.userRef.child('users').set({
       name: name
           });
      };

This will result with this data

{
  users:
   {
      name: "name"
   }
}

This happens because any null values you pass to the Set method will delete any data that was originally there.

Passing null to set() will remove the data at the specified location. https://www.firebase.com/docs/web/api/firebase/set.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!