简体   繁体   English

如何在AngularFire2中推送数组中的元素?

[英]How to push element in array in AngularFire2?

I am trying to update array in Firebase db. 我正在尝试更新Firebase数组中的数组。 Here is the Firebase user collection. 这是Firebase用户集合。 I am trying to create a group and updating id in users/groups collection. 我正在尝试创建一个组并更新用户/组集合中的ID。

"users" : {
    "F0m0l0JhIXMhlQmEMv5ci1MapLM2" : {
      "address" : "Address 123334",
      "displayName" : "Prashant 1",
      "email" : "fireapps@gmail.com",
      "groups" : [ "-KbzjEnTCGAto8SQbvzc", "-KbzjCLP3Aw2jfa0NmHO" ],
      "name" : "Prashant",
      "phone" : "9900XXXX190"
    }
  }

I want to add new key in groups array using AngularFire2. 我想使用AngularFire2在groups数组中添加新键。 How to update the array? 如何更新阵列?

I suppose you are using angularefire2, so update groups node would be 我想你正在使用angularefire2,所以更新groups节点将是

...
constructor(
  private af: AngularFire,
  ) {}
...
this.af.database.object('/users/F0m0l0JhIXMhlQmEMv5ci1MapLM2').update({
    "groups": "your new key" // <--------
  })
  .catch (error =>{
    console.log(error);
});

If you want to use your own keys then: 如果您想使用自己的密钥,那么:

this.af.database.object('/users/F0m0l0JhIXMhlQmEMv5ci1MapLM2/groups').update({
  "0": "group 1 key",
  "1": "group 2 key",
  "custom key": "group x key"
}).catch(error => { console.log(error); });

If you have your data in an array named updateArray , then update groups node with such array. 如果将数据放在名为updateArray的数组中,则使用此类数组更新groups节点。 Something likes 有点喜欢

this.af.database.object('/users/F0m0l0JhIXMhlQmEMv5ci1MapLM2/groups').update(updateArray).catch(error =>{ console.log(error); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM