如何获取 Firestore 上集合的每个文档的一个字段?
How to get one field of every document of a collection on Firestore?
我正在尝试获取一个集合的每个文档的仅一个 字段。我正在做的是做一个订阅方法来填充动态过滤器的列表,但我知道它不起作用,获得我想要的东西的最佳方法是什么?我想订阅它,这是我的代码:
this.af.collection('objects').valueChanges().subscribe(data => {
this.listTitles.push(data.title)
});
.subscribe
不适用于您的情况,可能您需要像这样拆分代码,
this.objectCollectionRef = this.af.collection('objects');
this.objectCollection = this.objectCollectionRef.valueChanges();
for(let data of this.objectCollection){
this.listTitles.push(data.title)
}
希望对您有所帮助!
只需映射结果并在模板中使用异步
class YourComponent{
listTitles:Observable<String>;
constructor(){
this.listTitles = this.af.collection('objects').valueChanges().pipe(
map(objs => objs.map(obj => obj.title))
)
}
}
在您的模板中:
<ng-container *ngFor="let item of listTitles | async">
<!-- have fun -->
</ng-container>
我正在尝试获取一个集合的每个文档的仅一个 字段。我正在做的是做一个订阅方法来填充动态过滤器的列表,但我知道它不起作用,获得我想要的东西的最佳方法是什么?我想订阅它,这是我的代码:
this.af.collection('objects').valueChanges().subscribe(data => {
this.listTitles.push(data.title)
});
.subscribe
不适用于您的情况,可能您需要像这样拆分代码,
this.objectCollectionRef = this.af.collection('objects');
this.objectCollection = this.objectCollectionRef.valueChanges();
for(let data of this.objectCollection){
this.listTitles.push(data.title)
}
希望对您有所帮助!
只需映射结果并在模板中使用异步
class YourComponent{
listTitles:Observable<String>;
constructor(){
this.listTitles = this.af.collection('objects').valueChanges().pipe(
map(objs => objs.map(obj => obj.title))
)
}
}
在您的模板中:
<ng-container *ngFor="let item of listTitles | async">
<!-- have fun -->
</ng-container>