Firebase 如何阅读子集合';将Firestore中的文档字段设置为react native

Firebase 如何阅读子集合';将Firestore中的文档字段设置为react native,firebase,react-native,google-cloud-firestore,react-native-firebase,Firebase,React Native,Google Cloud Firestore,React Native Firebase,尝试从react本机项目中firebase的Firestore的根级集合中的文档中读取所有子集合。不太确定要遵循哪些文档(web不能执行getCollections()/node?)。Firebase已导入,我已成功从firestore检索到其他数据,但我从未能够读取子集合数据。这不是在使用库react native firebase(尽管我已经尝试使用react native firebase,但它也没有解决此问题的文档化解决方案),不管怎样,我尝试了: componentDidMount()

尝试从react本机项目中firebase的Firestore的根级集合中的文档中读取所有子集合。不太确定要遵循哪些文档(web不能执行
getCollections()
/node?)。Firebase已导入,我已成功从firestore检索到其他数据,但我从未能够读取子集合数据。这不是在使用库
react native firebase
(尽管我已经尝试使用react native firebase,但它也没有解决此问题的文档化解决方案),不管怎样,我尝试了:

componentDidMount() {
    firebase
      .firestore()
      .collection('users')
      .doc(this.props.user.uid)
      .getCollections('conversations')
      .then(collections => {
        collections.forEach(collection => {
          alert(collection.id)
        })
      }) 
}
上面返回的
“\u firebase.default.firestore().collection(“users”).doc(this.props.user.uid)。getCollections”未定义

也尝试过:

componentDidMount() {
    firebase
      .firestore()
      .collection("users")
      .doc(this.props.user.uid)
      .collection("conversations")
      .get()
      .then(collections => {
        collections.forEach(collection => {
          alert(JSON.stringify(collection)); //collection.id is can be read here
        });
      });
在上面的示例中,可以读取集合id,但如何读取文档字段?上面给出了循环结构错误

警报(collection.data())
向我提供
[object object]

alert(JSON.stringify(collection.data())
给了我循环结构错误

这是firestore:

实际应用程序将填充给定用户的所有对话,然后填充给定对话的所有消息。 如何从react原生项目中的Firestore的所有子集合中读取数据?

请尝试下面的方法

async _getUserDataFromFirestore() {
        try {
          const ref = firebase
            .firestore()
            .collection('user')
            .doc(this.props.user.uid);
          await ref.get().then(userData => {
           console.log('User details of userID - ' + this.props.user.uid , userData.data());
          });  
        } catch (err) {
          console.log('Error while getting user data from firestore : ', err);
        }
      }

componentDidMount

中添加并调用此函数,以读取子集合文档数据最终起作用的是:

_getConversation() {
    firebase
      .firestore()
      .collection("users")
      .doc(this.props.user.uid)
      .collection("conversations")
      .get()
      .then(querySnapshot => {
        querySnapshot.forEach(queryDocumentSnapshot => {
          alert(queryDocumentSnapshot.get("members"));
        });
      })
      .catch(err => {
        alert(err);
      });
  }


深入研究文档确实更有帮助

我的目标不是“用户”集合。我在阅读文档/根级别集合时没有问题。我的问题是获取子集合的数据。在这种情况下,“对话”子集合
_getMessages() {
    firebase
      .firestore()
      .collection("users")
      .doc(this.props.user.uid)
      .collection("conversations")
      .doc("some-document-id-here")
      .collection("messages")
      .get()
      .then(querySnapshot => {
        querySnapshot.forEach(queryDocumentSnapshot => {
          alert(queryDocumentSnapshot.get("content"));
        });
      });
  }