使用Firestore文档和收集数据

时间:2019-06-21 06:58:30

标签: javascript reactjs firebase google-cloud-firestore

我在Firestore中有一些具有以下收集结构的文档

_fl_meta_: Object { createdBy: "Kz5vTvzlmGhRCxqKuDhrvvANqPe2", docId: "76qlSYWItERvJVnLKSDt", env: "production", … }
author: "asd"
content: "last 1"
date: "2019-06-19T12:00:00-07:00"
id: "76qlSYWItERvJVnLKSDt"
imageDeck: Array [ {…} ]
order: 0
parentId: 0
status: "published"
summary: "last 1"
title: "last 1"

im试图遍历我的文档并将其收集数据呈现给我的组件,但是出现问题。

我的query

const db = firebase.firestore();
db
  .collection('fl_content')
  .where('_fl_meta_.schema', '==', 'blog')
  .get()
  .then(querySnapshot => {
    querySnapshot.forEach(doc => {
      console.log(doc.data());
      // let results = Object.keys(doc.data()).map(function (key) {
      //   return [String(key), doc.data()[key]];
      // })
      // this.setState({ cards: results });
    });
  })
  .catch(error => {
    console.log("Error getting documents: ", error);
  });
该查询的

firestore输出上述集合。谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

获取 @Query ( "SELECT * FROM t_ql_quest_category") fun getQuestCategoryCursor(): Cursor 并将其映射为包含文档的对象数组。

querySnapshot

然后您可以执行以下操作:

const querySnapshot = firebase.firestore().collection('fl_content')
  .where('_fl_meta_.schema', '==', 'blog').get()

.then((querySnapshot) => {
  // The following line will result in an array of objects (each object is your document data)
  const yourDocuments = querySnapshot.docs.map((doc) => doc.data());
  this.setState({cards: yourDocuments});
})
.catch((error) => {
  console.log(error);
});