Map items of collection snapshot in Firebase Firestore

前端 未结 5 430
面向向阳花
面向向阳花 2020-12-24 11:39

Firebase Firestore Guides show how to iterate documents in a collection snapshot with forEach:

db.collection(\"cities\").get().then(function(que         


        
5条回答
  •  难免孤独
    2020-12-24 12:15

    Got pretty sick and tired of Firestore returning stuff in their classes or whatever. Here's a helper that if you give it a db and collection it will return all the records in that collection as a promise that resolves an actual array.

    const docsArr = (db, collection) => {
      return db
        .collection(collection)
        .get()
        .then(snapshot => snapshot.docs.map(x => x.data()))
    }
    
    ;(async () => {
      const arr = await docsArr(myDb, myCollection)
      console.log(arr)
    })()
    

提交回复
热议问题