Getting all documents from one collection in Firestore

前端 未结 8 1921
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:19

    Try following LOCs

        let query = firestore.collection('events');
        let response = [];
        await query.get().then(querySnapshot => {
              let docs = querySnapshot.docs;
              for (let doc of docs) {
                  const selectedEvent = {
                         id: doc.id,
                         item: doc.data().event
                      };
                 response.push(selectedEvent);
              }
       return response;
    
    0 讨论(0)
  • 2020-12-23 16:28

    Here's a simple version of the top answer, but going into an object with the document ids:

    async getMarker() {
        const snapshot = await firebase.firestore().collection('events').get()
        return snapshot.docs.reduce(function (acc, doc, i) {
                  acc[doc.id] = doc.data();
                  return acc;
                }, {});
    }
    
    0 讨论(0)
  • 2020-12-23 16:33

    if you need to include the key of the document in the response, another alternative is:

    async getMarker() {
        const snapshot = await firebase.firestore().collection('events').get()
        const documents = [];
        snapshot.forEach(doc => {
           documents[doc.id] = doc.data();
        });
        return documents;
    }
    
    0 讨论(0)
  • 2020-12-23 16:34

    I made it work this way:

    async getMarkers() {
      const markers = [];
      await firebase.firestore().collection('events').get()
        .then(querySnapshot => {
          querySnapshot.docs.forEach(doc => {
          markers.push(doc.data());
        });
      });
      return markers;
    }
    
    0 讨论(0)
  • 2020-12-23 16:35

    if you want include Id

    async getMarkers() {
      const events = await firebase.firestore().collection('events')
      events.get().then((querySnapshot) => {
          const tempDoc = querySnapshot.docs.map((doc) => {
            return { id: doc.id, ...doc.data() }
          })
          console.log(tempDoc)
        })
    }
    

    Same way with array

    async getMarkers() {
      const events = await firebase.firestore().collection('events')
      events.get().then((querySnapshot) => {
          const tempDoc = []
          querySnapshot.forEach((doc) => {
             tempDoc.push({ id: doc.id, ...doc.data() })
          })
          console.log(tempDoc)
       })
     }
    
    0 讨论(0)
  • 2020-12-23 16:36

    You could get the whole collection as an object, rather than array like this:

    async getMarker() {
        const snapshot = await firebase.firestore().collection('events').get()
        const collection = {};
        snapshot.forEach(doc => {
            collection[doc.id] = doc.data();
        });
        return collection;
    }
    

    That would give you a better representation of what's in firestore. Nothing wrong with an array, just another option.

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