【问题标题】:Firestore delete a field in an object is a field of another object inside an array [duplicate]Firestore删除对象中的字段是数组中另一个对象的字段[重复]
【发布时间】:2019-08-26 09:49:59
【问题描述】:

我有一个这样的模型

{
   "actions": [
      {
         "type": "1",
         "value": {
            "abc": {
               "1": "true"
            },
            "def": {
               "1": "true"
            }
         }
      }
   ]
}

我想删除 abc 字段

这是我正在使用的代码,但它对我不起作用(我也在使用批量写入来更新多个文档)。

// getScene's data received from firestore
let batch = this.firebase.firestore().batch();
getScene.forEach(o => {
            let removeDeviceFromScene = this.sceneDoc.doc(o.id);
            const fieldPath = `actions.value.abc`;
            batch.update(removeDeviceFromScene, {
                    filePath: this.firebase.firestore.FieldValue.delete()
                }
            }); await batch.commit().then(() => {
            console.log(`Begin write batch`);
        });

我也试过这样,但结果一样

let update = {};
let removeDeviceFromScene = this.sceneDoc.doc(o.id);
update["actions.value.abc"] = this.firebase.firestore.FieldValue.delete();
batch.update(removeDeviceFromScene, update);

我的代码有问题吗,有解决这个问题的办法吗?

提前致谢

【问题讨论】:

标签: typescript firebase google-cloud-firestore


【解决方案1】:

我之前说的不是真的。无法更新 Firestore 文档中数组中的单个元素。见How to Add/Update/Remove array elements in firebase firestore android using Hashmap? A Store Database

由于actions 是一个数组,您需要指定要更新的元素的数组索引。所以:update["actions.0.value.abc"] = ...

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 2020-04-02
    • 2020-11-25
    • 2019-02-22
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    相关资源
    最近更新 更多