【问题标题】:Firestore FieldValue.delete not deleting map field in transactionFirestore FieldValue.delete 不删除事务中的地图字段
【发布时间】:2021-09-28 04:45:24
【问题描述】:

在我的 ionic 5 应用程序中,当我直接在事务中更新 Firestore 文档时,以下对象的行为有所不同。

let upDoc = {
          user: firestore.FieldValue.arrayRemove(this.uid),
          ["userName." + this.uid]: firestore.FieldValue.delete()
        }

所以,user 是一个数组,userName 是一个映射。我正在尝试从数组中删除用户 ID 并从地图中删除用户名。

当我像下面这样直接执行时,两者都会被删除。

 this.afs.doc('user/1234/').update(upDoc);

但是当我在事务中执行相同操作时,只有 user 数组中的数据被删除,但 userName 地图数据不会被删除。

const doc = this.afs.firestore.doc('user/1234/');
await this.afs.firestore.runTransaction(function (transaction) {
  return transaction.get(doc).then(function (document) {
    transaction.set(doc, upDoc, {merge: true});
  })
});

【问题讨论】:

    标签: angular firebase ionic-framework google-cloud-firestore transactions


    【解决方案1】:

    您是否尝试过使用update 方法而不是set

    await firebase.firestore().runTransaction(function (transaction) {
      return transaction.get(doc).then(function (document) {
        transaction.update(doc, upDoc);
      })
    });
    

    我能够复制您的问题并使用update 方法做到了。我不完全确定原因,但我正在调查。

    【讨论】:

    • 谢谢! update 正在工作并且可以作为一种解决方法,但 set 不是。
    猜你喜欢
    • 2018-07-28
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 2020-09-11
    • 2018-06-17
    • 2021-11-21
    相关资源
    最近更新 更多