我尝试使用documentation中定义的get()方法迭代集合中的所有文档,但它并不适用于我。我收到get is not a function
错误,我做错了什么?
先谢谢
export class MainComponent implements OnInit {
selectedCharacter: number = null;
people: Observable<any>;
private peopleCollection: AngularFirestoreCollection<Character>;
constructor(private db: AngularFirestore,
private route: ActivatedRoute,
private location: Location) {
this.peopleCollection = db.collection('people');
this.people = this.peopleCollection.valueChanges();
this.route.params.subscribe(
params => (this.selectedCharacter = +params['id'])
);
}
ngOnInit() {
this.peopleCollection.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
});
}
}
答案 0 :(得分:2)
TL; DR:this.peopleCollection。 ref .get()
收集方法valueChanges
会返回Observable
:
export declare class AngularFirestoreCollection<T> {
...
valueChanges(events?: DocumentChangeType[]): Observable<T[]>;
...
}
您可以订阅Observable
返回的valueChanges
:
ngOnInit() {
this.people.subscribe(data => console.log(data));
}
或者您可以使用CollectionReference
获取Promise
:
ngOnInit() {
this.peopleCollection.ref.get().then(data => console.log(data));
}