【问题标题】:Get data from a collection with firestore ionic 4使用 firestore ionic 4 从集合中获取数据
【发布时间】:2019-05-12 17:07:28
【问题描述】:

我想从带有 firebase firestore 的集合中的 doc 中获取唯一数据

所以我用来获取所有数据:

 ngOnInit() {

return this.firestore.collection('users').snapshotChanges()



.subscribe(data => {


  console.log(data);
  this.utilisateurs = data;
  console.log(this.passided);

});


}

这会得到唯一的 id:

this.passided = this.navParams.get('id');

我试着这样做:

返回this.firestore.collection('users').doc(this.passided).snapshotChanges()

但是不工作,你能帮帮我吗?

【问题讨论】:

  • 在您使用 this.firestore.collection('users').doc(this.passided).snapshotChanges() 的位置添加代码,并在 Firebase 控制台中添加您的 Firestore 屏幕截图
  • 添加图片
  • 你是否得到了 id 值 console.log(this.passided);?
  • 当然是 "tAB12BdparcelpbXEWRm"

标签: firebase ionic-framework ionic4


【解决方案1】:

snapshotChanges() 是类AngularFirestoreCollection 中的一个方法,它以DocumentChangeAction 的形式返回一个Observable 数据。

如果你想操作文档,那么你可以使用以下方法:

set(data: T) - 破坏性地更新文档的数据。

update(data: T) - 无损更新文档数据。

delete() - 删除整个文档。不删除任何嵌套集合。


因此,这个this.firestore.collection('users').doc(this.passided).snapshotChanges() 将不起作用,因为snapshotChanges() 不是document.ts 中的方法

供参考: https://github.com/angular/angularfire2/blob/master/docs/firestore/documents.md#snapshotchanges

https://github.com/angular/angularfire2/blob/master/src/firestore/collection/collection.ts#L97

【讨论】:

  • 最后一个 console.log(data) 给出:{payload: DocumentSnapshot, type: "value"} payload: DocumentSnapshot {_firestore: Firestore, _key: DocumentKey, _document: Document, _fromCache: true, _hasPendingWrites: false} type: "value" proto: Object
  • 试试this.firestore.collection('users').doc(this.passided).valueChanges().subscribe(data => { console.log(data); });
  • this.firestore.collection('users').doc(this.passside).valueChanges().subscribe(data => { console.log(data); });我添加了它并在控制台中为我提供了数据库中包含 doc 的所有数据的好行,但是如何将其放入 html 中? this.utilisateurs = 数据; ?谢谢
  • 使用字符串插值例如<h3>Current customer: {{ currentCustomer }}</h3>
  • 或者通过this.docData = data; this.utilisateurs["documentData"] = this.docData;将其添加到对象this.utilisateurs
猜你喜欢
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2018-07-13
  • 2020-11-26
  • 2021-10-31
相关资源
最近更新 更多