在本机项目中,尝试从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
});
});
在上面,可以读取集合ID,但是如何读取文档字段?上面给了我循环结构错误。
alert(collection.data())
给我 [object Object]
alert(JSON.stringify(collection.data())
给我循环结构错误
这是消防站:
实际的应用将是填充给定用户的所有对话,然后填充给定对话的所有消息。如何在本机项目中从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"));
});
});
}
更深入地研究文档确实更有帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句