Getting all documents from one collection in Firestore

前端 未结 8 1923
Happy的楠姐
Happy的楠姐 2020-12-23 15:59

Hi I\'m starting with javascript and react-native and I\'m trying to figure out this problem for hours now. Can someone explain me how to get all the documents from firestor

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 16:41

    I prefer to hide all code complexity in my services... so, I generally use something like this:

    In my events.service.ts

        async getEvents() {
            const snapchot = await this.db.collection('events').ref.get();
            return new Promise  (resolve => {
                const v = snapchot.docs.map(x => {
                    const obj = x.data();
                    obj.id = x.id;
                    return obj as Event;
                });
                resolve(v);
            });
        }
    

    In my sth.page.ts

       myList: Event[];
    
       construct(private service: EventsService){}
    
       async ngOnInit() {
          this.myList = await this.service.getEvents();
       }
    
    

    Enjoy :)

提交回复
热议问题