简体   繁体   English

未检索React Native Firestore子集合

[英]Not Retrieved React Native Firestore Subcollection

I have a problem when i want to get subcollections. 我想获取子集合时遇到问题。 Can anybody help me, please ? 有人可以帮我吗?

firestore structure : 消防站结构:

questions(collection) ->  curretUser.id(doc) -> questions(collection) -> questionIDautoCreated(doc)-> {title: "", description: "" }

And i try to get data: 我尝试获取数据:

return (dispatch) => {
  firebase.firestore().collection('questions').doc(currentUser.uid)
  .collection('questions').get()
  .then((documentSnapshot) => {
    const value = documentSnapshot.data();
    console.log('Value Succeed : ');
    console.log(value);
  });
};

Error : 错误:

Possible Unhandled Promise Rejection (id:0) Type Error: documentSnapshot.data is not a function 可能的未处理的承诺拒绝(id:0) 类型错误:documentSnapshot.data不是函数

Thank You 谢谢

Because collection is not a DocumentSnapshot , its a QuerySnapshot . 因为collection不是DocumentSnapshot ,所以它是QuerySnapshot Collection contains a list of data, it is not an actual document. 集合包含数据列表,它不是实际文件。 If you look in the firestore docs here . 如果您在这里查看firestore文档。 You can see when querying for collection, you should: 您可以在查询集合时看到,您应该:

querySnapshot.forEach(function(doc) {
    console.log(doc.id, " => ", doc.data());
});

The exception speaks for it self, the object you receive is not the one you expect. 异常说明了这一点,您收到的对象不是您期望的对象。

return (dispatch) => {
  firebase.firestore().collection('questions').doc(currentUser.uid)
   .collection('questions').get()
   .then((querySnapshot) => {
     querySnapshot.forEach((doc) => {
       console.log(doc.id, " => ", doc.data());
     });
   });
 };

You can get subcollections like this, 你可以得到这样的子集合,

Collections returns docs array, you must access docs data first. 集合返回docs数组,必须首先访问docs数据。 Quick Example 快速范例

firebase.firestore().collection('...').get().then(s => console.log(s.docs))

Your solution 您的解决方案

return (dispatch) => {
    firebase.firestore().collection(`questions/${currentUser.id}/questions`)
      .get()
      .then(questionSnapshot => questionSnapshot.docs)
      .then(questions => {
        questions.forEach(question => {
          console.log(question)
        })
      })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM