【问题标题】:How to read a sub-collection's document fields from Firestore in react-native如何在 react-native 中从 Firestore 读取子集合的文档字段
【发布时间】:2020-02-15 19:06:23
【问题描述】:

尝试从 react-native 项目中的 firebase 的 Firestore 的根级集合内的文档中读取所有子集合。不太确定要遵循哪个文档(网络不能做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)
        })
      }) 
}

以上返回'_firebase.default.firestore().collection("users").doc(this.props.user.uid).getCollections' is undefined

也试过了:

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
        });
      });

在上面,可以读取collection id,但是如何读取文档字段呢?以上给了我循环结构错误。

alert(collection.data()) 给我[object Object]

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

这里是火库:

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

【问题讨论】:

    标签: firebase react-native google-cloud-firestore react-native-firebase


    【解决方案1】:

    要读取子集合文档数据,最终起作用的是:

    _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"));
            });
          });
      }
    

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

    【讨论】:

      【解决方案2】:

      你好试试下面

      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添加调用这个函数

      【讨论】:

      • “用户”集合不是我要定位的集合。我在阅读文档/根级集合时没有问题。我的问题是关于获取子集合的数据。在这种情况下,“对话”子集合
      猜你喜欢
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2021-11-04
      • 1970-01-01
      • 2019-11-03
      相关资源
      最近更新 更多