【问题标题】:Batch update works as set operation批量更新作为集合操作
【发布时间】:2021-11-05 12:07:51
【问题描述】:

我需要使用 Firebase 的实时数据库进行批量更新。我是这样做的:

这工作正常。但我只需要第二次操作的更新。此时它作为set 操作。有什么线索吗?

 const updates = {}; //batch operation

   // this is working fine and I need it as set operation
    updates[`groups/${this.groupId}/leads/${lead.id.toString()}`] = {
      name: lead.name,
      address: lead.address,
    };

    // But problem is here. I need to write it as update operation
    updates[`groups/${this.groupId}/addresses/${lead.id.toString()}`] = {
      address: lead.address,
     };

    return this.angularFireDatabase.object('/').update(updates);

【问题讨论】:

    标签: angular typescript firebase firebase-realtime-database angularfire


    【解决方案1】:

    在您传入的映射中的每个单独键上,多路径更新的作用是 set。由于您传入 groups/${this.groupId}/addresses/${lead.id.toString()} 作为键,它设置您指定的值在那条路上。

    如果您只想更新该路径下的address 属性,请将address 键包含在路径中,而不是值中:

    updates[`groups/${this.groupId}/addresses/${lead.id.toString()}/address`] = lead.address;
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多