Firestore get()集合中的所有文档都会返回错误

时间:2018-05-06 07:55:42

标签: javascript angular firebase google-cloud-firestore

我尝试使用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());
            });
        });
   }
}

1 个答案:

答案 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));
}