【问题标题】:How can I get a particular document data from fire store collection by ID?如何按 ID 从 Firestore 集合中获取特定文档数据?
【发布时间】:2023-03-03 05:07:20
【问题描述】:

我需要根据 Id 从集合中获取数据,但我无法获取详细信息。 我尝试了某些方法,但都没有达到预期效果。

示例 1

return this.db.collection("customers/", id).snapshotChanges(); 

它说要传递奇数个段

我的功能

this.auth.getUserDetails(this.userid).subscribe(data => {
      console.log(data);
    });

示例 2

return this.db.collection('customers')
      .doc(id)
      .get();

获取和显示数据的更好方法是什么?

【问题讨论】:

  • 示例 1 肯定会失败。示例 2 有什么问题?只要id 有一个有效的文档 ID 字符串,它看起来就可以工作。

标签: javascript angular ionic-framework google-cloud-firestore angularfire2


【解决方案1】:

你可以这样:

// Assuming ID is passed as an argument.
this.db.doc(`costumers/${id}`).snapshotChanges(); 

您遇到问题的原因是因为您使用的是collection 而不是doc

【讨论】:

  • 您可以使用 .collection().doc() (我一直这样做)...但是...提供给 .doc() 的字符串需要是 PATH,而不仅仅是一个 ID,所以:返回 this.db.collection('customers').doc("/" + id).get();
猜你喜欢
  • 2018-04-04
  • 2021-07-17
  • 2020-12-27
  • 2019-09-24
  • 2022-10-12
  • 2021-12-16
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
相关资源
最近更新 更多