从Firestore中的一个集合中获取所有文档

Vlt*_*Vlt 24 javascript firebase react-native google-cloud-firestore

嗨,我开始使用javascript和react-native,我正试图解决这个问题几个小时了.有人可以解释我如何从firestore集合中获取所有文档吗?

我一直在尝试这个:

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;
}
Run Code Online (Sandbox Code Playgroud)

Log 1打印所有对象(逐个)但是log 2未定义,为什么?

Dou*_*son 56

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

async getMarker() {
    const snapshot = await firebase.firestore().collection('events').get()
    return snapshot.docs.map(doc => doc.data());
}
Run Code Online (Sandbox Code Playgroud)

  • @etoxin 只有当你的导入/需求与OP正在做的不同时才有效。 (3认同)
  • @Mentor无论您对代码中的快照如何处理,查询都会对每个匹配的文档进行读取。 (2认同)
  • @bermick 一个查询是一个往返。查询的整个结果都会返回到快照中。 (2认同)

小智 23

如果你想包括 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)
    })
}
Run Code Online (Sandbox Code Playgroud)

与数组相同的方式

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)
   })
 }
Run Code Online (Sandbox Code Playgroud)


dan*_*y74 10

文档指出

import { collection, getDocs } from "firebase/firestore";

const querySnapshot = await getDocs(collection(db, "cities"));
querySnapshot.forEach((doc) => {
  // doc.data() is never undefined for query doc snapshots
  console.log(doc.id, " => ", doc.data());
});
Run Code Online (Sandbox Code Playgroud)

不过我正在使用以下内容(请原谅 TypeScript):

import { collection, Firestore, getDocs, Query, QueryDocumentSnapshot, QuerySnapshot } from 'firebase/firestore'

const q: Query<any> = collection(db, 'videos')
const querySnapshot: QuerySnapshot<IVideoProcessed> = await getDocs(q)
const docs: QueryDocumentSnapshot<IVideoProcessed>[] = querySnapshot.docs
const videos: IVideoProcessed[] = docs.map((doc: QueryDocumentSnapshot<IVideoProcessed>) => doc.data())
Run Code Online (Sandbox Code Playgroud)

其中 db 具有类型Firestore


Vlt*_*Vlt 7

我让它这样工作:

async getMarkers() {
  const markers = [];
  await firebase.firestore().collection('events').get()
    .then(querySnapshot => {
      querySnapshot.docs.forEach(doc => {
      markers.push(doc.data());
    });
  });
  return markers;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您需要在响应中包含文档的键,另一种选择是:

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


One*_*ood 6

您可以将整个集合作为对象,而不是像这样的数组:

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

这将使您更好地了解 firestore 中的内容。数组没有任何问题,只是另一种选择。