【问题标题】:How can I get specific document data from firestore querysnapshot?如何从 firestore 查询快照中获取特定的文档数据?
【发布时间】:2018-11-14 11:25:20
【问题描述】:

我在函数中有一个查询快照。 并希望将整个查询快照带到另一个函数(functionTwo)。 在functionTwo 中,我想在WITHOUT forEach 的querysnapshot 中获取特定文档。具体的文档可以根据不同的情况进行更改。

ref_serial_setting.get()
    .then(querysnapshot => {
      return functionTwo(querysnapshot)
    })
    .catch(err => {
      console.log('Error getting documents', err)
    })


let functionTwo = (querysnapshot) => {
  // getting value

  const dataKey_1 = "dataKey_1"

  // Tried 1
  const value = querysnapshot.doc(dataKey_1).data()

  // Tried 2
  const value = querysnapshot.document(dataKey_1).data()

  // Tried 3 (Put 'data_name': dataKey_1 in that doc)
  const value = querysnapshot.where('data_name', '==', dataKey_1).data()
}

结果是所有这些尝试都不是功能。

如何从查询快照中获取特定的文档数据??

有什么简单的方法可以将查询快照更改为 JSON?

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您可以使用QuerySnapshotdocs 属性获取文档快照的数组。之后,您必须循环获取文档快照的数据以查找您的文档。

    const docSnapshots = querysnapshot.docs;
    
    for (var i in docSnapshots) {
        const doc = docSnapshots[i].data();
    
        // Check for your document data here and break when you find it
    }
    

    或者如果您实际上不需要完整的QuerySnapshot,您可以使用where 函数在查询上调用get 应用过滤器对象:

    const dataKey_1 = "dataKey_1";    
    const initialQuery = ref_serial_setting;
    const filteredQuery = initialQuery.where('data_name', '==', dataKey_1);
    
    filteredQuery.get()
        .then(querySnapshot => {
            // If your data is unique in that document collection, you should
            // get a query snapshot containing only 1 document snapshot here
        })
    
        .catch(error => {
            // Catch errors
        });
    

    【讨论】:

    • QuerySnapshot 对象的所有信息都可用here
    • 感谢您的回复!我知道查询快照中的确切文档名称。我还需要遍历“docSnapshots”来获取里面的数据吗? // 我需要完整的 QuerySnapshot,因为我有一些其他功能可以从其他地方获取确切的文档名称。
    • 我明白了,不幸的是,如果您需要完整的QuerySnapshot,那么您将不得不遍历每个文档快照并搜索名称。这只是 Firestore 在返回的查询中包装数据的本质。对不起,我不能给你你想听到的答案,但目前,QuerySnapshots 不支持搜索或过滤方法。
    【解决方案2】:

    有一个简单的方法可以做到这一点,每个QuerySnapshot 都有一个属性docs,它返回一个QueryDocumentSnapshots 的数组。请参阅QuerySnapshot 文档。

    let citiesRef = db.collection('cities');
    let query = citiesRef.where('capital', '==', true).get().then(snapshot => {
      snapshot.docs[0]; // => returns first document
    });
    

    【讨论】:

      【解决方案3】:
      let citiesRef = db.collection('cities');
      let query = citiesRef.where('capital', '==', true).get()
        .then(snapshot => {
          if (snapshot.empty) {
            console.log('No matching documents.');
            return;
          }  
      
          snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
          });
        })
        .catch(err => {
          console.log('Error getting documents', err);
        });
      

      来自https://firebase.google.com/docs/firestore/query-data/get-data

      【讨论】:

      • 谢谢,这个 forEach 对我来说真的很好用!关于我无法工作的循环的其他答案。
      猜你喜欢
      • 2019-08-02
      • 2019-09-03
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      相关资源
      最近更新 更多