简体   繁体   English

如何在Firestore上获取集合的每个文档的一个字段?

[英]How to get one field of every document of a collection on Firestore?

I'm trying to get just one field of every document of one collection. 我试图让只有一个一个集合的每个文档的领域。 What i'm doing is to do a subscribe method to fill a list for a dynamic filter, but i know it doesnt work, what is the best way to get just what i want? 我正在做的是一种订阅方法来填充动态过滤器列表,但是我知道它不起作用,什么才是我想要的最佳方法? I would like to be subscribed to it, here is my code: 我想订阅它,这是我的代码:

this.af.collection('objects').valueChanges().subscribe(data => {        
  this.listTitles.push(data.title)
});

.subscribe won't work in your case, probably you need to split your code like this, .subscribe在您的情况下不起作用,可能您需要像这样拆分代码,

this.objectCollectionRef = this.af.collection('objects');
this.objectCollection = this.objectCollectionRef.valueChanges();


for(let data of this.objectCollection){
   this.listTitles.push(data.title)
}

Hope this helps! 希望这可以帮助!

Just map the result and use async in your template 只需映射结果并在模板中使用异步

class YourComponent{
  listTitles:Observable<String>;

  constructor(){
    this.listTitles = this.af.collection('objects').valueChanges().pipe(
      map(objs => objs.map(obj => obj.title))
    )
  }
}

And in your template : 在您的模板中:

<ng-container *ngFor="let item of listTitles | async">
 <!-- have fun -->
</ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM