Getting all documents from one collection in Firestore

前端 未结 8 1922
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:38

    The example in the other answer is unnecessarily complex. This would be more straightforward, if all you want to do is return the raw data objects for each document in a query or collection:

    async getMarker() {
        const snapshot = await firebase.firestore().collection('events').get()
        return snapshot.docs.map(doc => doc.data());
    }
    
    0 讨论(0)
  • 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 <Event[]> (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 :)

    0 讨论(0)
提交回复
热议问题