admin管理员组文章数量:1421046
I'm trying to iterate all the documents in the collection with the get() method as defined in the documentation, however it doesn't work for me. I get a get is not a function
error, what am I doing wrong?
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());
});
});
}
}
I'm trying to iterate all the documents in the collection with the get() method as defined in the documentation, however it doesn't work for me. I get a get is not a function
error, what am I doing wrong?
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());
});
});
}
}
Share
Improve this question
edited Jul 9, 2019 at 4:35
Arnaud
7,45910 gold badges52 silver badges72 bronze badges
asked May 6, 2018 at 7:55
mwomwo
851 silver badge7 bronze badges
1 Answer
Reset to default 5TL;DR : this.peopleCollection.ref.get()
The collection method valueChanges
returns an Observable
:
export declare class AngularFirestoreCollection<T> {
...
valueChanges(events?: DocumentChangeType[]): Observable<T[]>;
...
}
You could subscrible to the Observable
returned by valueChanges
:
ngOnInit() {
this.people.subscribe(data => console.log(data));
}
Or you can use the CollectionReference
to get the Promise
:
ngOnInit() {
this.peopleCollection.ref.get().then(data => console.log(data));
}
本文标签: javascriptFirestore get() all documents in a collection returns an errorStack Overflow
版权声明:本文标题:javascript - Firestore get() all documents in a collection returns an error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745318698a2653274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论