Javascript 从 Firestore 中的一个集合中获取所有文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/52100103/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all documents from one collection in Firestore
提问by Vlt
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 firestore collection ?
嗨,我从 javascript 和 react-native 开始,我现在正试图解决这个问题几个小时。有人可以解释我如何从 firestore 集合中获取所有文件吗?
I have been trying this:
我一直在尝试这个:
async getMarkers() {
  const events = await firebase.firestore().collection('events').get()
    .then(querySnapshot => {
      querySnapshot.docs.map(doc => {
        console.log('LOG 1', doc.data());
        return doc.data();
      });
    });
  console.log('LOG 2', events);
  return events;
}
Log 1 prints all the objects(one by one) but log 2 is undefined, why ?
日志 1 打印所有对象(一个接一个),但日志 2 未定义,为什么?
回答by Doug Stevenson
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());
}
回答by Imanullah
if you want include Id
如果你想包括 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)
   })
 }
回答by Vlt
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;
}
回答by Rodrigo
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;
}
回答by OneHoopyFrood
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.
这将使您更好地表示 firestore 中的内容。数组没有错,只是另一种选择。
回答by Nowdeen
I prefer to hide all code complexity in my services... so, I generally use something like this:
我更喜欢在我的服务中隐藏所有代码复杂性......所以,我通常使用这样的东西:
In my events.service.ts
在我的 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
在我的 sth.page.ts
   myList: Event[];
   construct(private service: EventsService){}
   async ngOnInit() {
      this.myList = await this.service.getEvents();
   }
Enjoy :)
享受 :)

