【问题标题】:How get document path in order to delete/edit a document in firestore?如何获取文档路径以删除/编辑 Firestore 中的文档?
【发布时间】:2017-10-06 03:37:46
【问题描述】:

我正在使用 angularfire2 使用 ionic 测试新的 firestore,我能够按照文档列出和创建集合和文档。

这些是我用来列出文档集合的ts and html 文件。

根据github 中的文档,我可以使用AngularFirestoreDocument 中的delete() 和update() 方法,但由于我有一组文档,我需要文档路径来执行类似this.chemicalsCollection.doc('path/1).delete() 或@ 的操作987654326@.

在过去(上周)我可以这样做:

constructor(database: AngularFireDatabase) {
  this.itemsRef$ = this.database.list('items');
}
deleteItem(item) {
  this.itemsRef$.remove(item.$key)
}

并在 html *ngFor="let item of chemicalsRef$ | async" (click)="deleteItem(item)" 中循环 itemsRef$。索引$key 来自firebase 数据库,但如果我对firestore 执行相同操作,则对象中没有$keypath,只有我创建的数据。

我错过了什么?如何获取文档的路径?

【问题讨论】:

    标签: firebase angularfire2 google-cloud-firestore


    【解决方案1】:

    有多种方法可以从集合中流式传输数据。我猜你正在使用.ValueChanges(),它返回一个包含 JSON 对象数据的 Observable。 您需要使用的是.snapshotChanges()它返回一个包含更多信息的数组。然后,您可以使用映射运算符从数组中获取数据和对象 ID。

    snapshotChanges() 下的文档中显示了一个示例: AngularFire GitHub Doc

    【讨论】:

    • 我遵循了 snapshotChanges example 并且它有效。谢谢
    猜你喜欢
    • 2020-01-05
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多