【问题标题】:deleting specific part of field in firebase (Angular)删除firebase中字段的特定部分(Angular)
【发布时间】:2018-12-15 07:39:32
【问题描述】:

我正在尝试删除 firebase 中的部分对象。这是它在数据库中的样子:

这是在“用户”集合中的特定用户 ID 中。

我目前尝试删除它的方式:

  removeUserLikedProperty(property_id: string) {
    console.log(property_id)
    return fromPromise(this.usersCollection.doc(`${this._auth.currentUserId}/test/${property_id}`).delete());
  }

这是在我的服务范围内。然后我在我的 ts 文件中有这个:

  removeUserLikedProperty(property_id: string) {
    this._user.removeUserLikedProperty(property_id);
    console.log(property_id)
  }

然后最后在我的 html 中单击时调用它:

<button class="button button-previous" (click)="removeUserLikedProperty(property?.property_id)">unlike</button>

从我所阅读的所有内容中,这是我对如何删除字段成员的理解。通过访问用户集合,获取用户 ID,然后进入“测试”,然后删除特定 ID。有人对此有任何进一步的了解吗?可能语法可能完全关闭!

【问题讨论】:

    标签: javascript angular typescript firebase google-cloud-firestore


    【解决方案1】:

    您正在混合文档和字段。请Read the documentation about data model

    无论哪种方式,从图像中我了解到您想要实现的是删除嵌套对象中的一个字段。

    你需要知道两件事:how to delete a field in a documenthow to access a field in a nested object

    以下应该有效:

    removeUserLikedProperty(property_id: string) {
      console.log(property_id);
      return fromPromise(this.usersCollection.doc(`${this._auth.currentUserId}`).update({
          [`test.${property_id}`]: firebase.firestore.FieldValue.delete()
        })
      );
    }
    

    【讨论】:

    • 嘿,谢谢你的回答!几样东西。我收到此错误:webpack:编译失败。 src/app/shared/_services/user.service.ts(106,13) 中的错误:错误 TS1005:预期为“:”。 src/app/shared/_services/user.service.ts(106,25): 错误 TS1005: ',' 预期。 src/app/shared/_services/user.service.ts(106,35): 错误 TS1005: ':' 预期。 src/app/shared/_services/user.service.ts(108,6): 错误 TS1005: ')' 预期。
    • 另外,test 应该是一个字符串吗?
    • 对不起,代码是手写的。但是现在更正了语法。
    猜你喜欢
    • 2021-12-21
    • 2014-07-03
    • 1970-01-01
    • 2019-11-17
    • 2014-09-04
    • 2015-05-13
    • 1970-01-01
    相关资源
    最近更新 更多