gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:57:30 32 4
gpt4 key购买 nike

尝试从 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com