从Firestore中的一个集合中获取所有文档 [英] Getting all documents from one collection in Firestore

查看:59
本文介绍了从Firestore中的一个集合中获取所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从javascript和react-native开始,现在我想弄清楚这个问题已经有几个小时了.有人可以解释一下如何从Firestore集合中获取所有文档吗?

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 ?

我一直在尝试:

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;
}

日志1打印所有对象(一个一个地打印),但是日志2未定义,为什么?

Log 1 prints all the objects(one by one) but log 2 is undefined, why ?

推荐答案

另一个答案中的示例不必要地复杂.如果您要做的只是返回查询或集合中每个文档的原始数据对象,这将更简单:

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());
}

这篇关于从Firestore中的一个集合中获取所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆