firebase - 如何在 react-native 中从 Firestore 读取子集合的文档字段

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

尝试从 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' 未定义

也试过:

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,但是怎么读取document fields呢?以上给了我循环结构错误。

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

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

这里是 firestore:

enter image description here

enter image description here

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

最佳答案

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

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

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

关于firebase - 如何在 react-native 中从 Firestore 读取子集合的文档字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456701/

相关文章:

firebase - 删除 firebase cloud firestore 中的许多关系

firebase - 在服务器端调用 firestore 时,用户对 cloud firestore 的访问和数据控制

android - FirebaseRemoteConfig : Cannot resolve symbol

swift firebase 嵌套子项计数

reactjs - Reactotron 给出 "SKIPPED"来响应网络调用

image - React Native Image 元素因 URI 位置而崩溃

javascript - 使用从 API 调用获取的数据更新 TabNavigator 标签

node.js - 如何使用 Firebase 云功能发送自定义电子邮件

java - 有没有办法从客户端的 google oauth2.0 token 获取 firebase 身份验证 token

javascript - 如何使用 Firestore 在快照上按值排序