简体   繁体   English

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

[英]Firestore get() all documents in a collection returns an error

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. 我试图用迭代作为中定义的get()方法集合中的所有文档文件 ,但它不适合我的工作。 I get a get is not a function error, what am I doing wrong? 我得到的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());
            });
        });
   }
}

TL;DR : this.peopleCollection. TL; DR:this.peopleCollection。 ref .get() ref .get()

The collection method valueChanges returns an Observable : 收集方法valueChanges返回一个Observable

export declare class AngularFirestoreCollection<T> {
    ...
    valueChanges(events?: DocumentChangeType[]): Observable<T[]>;
    ...
}

You could subscrible to the Observable returned by valueChanges : 您可以订阅valueChanges返回的Observable

ngOnInit() {
    this.people.subscribe(data => console.log(data));
}

Or you can use the CollectionReference to get the Promise : 或者,您可以使用CollectionReference来获得Promise

ngOnInit() {
    this.peopleCollection.ref.get().then(data => console.log(data));
}

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

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