componentDidMount(){
dbh.collection('Groups').get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
this.setState({
groups: [...this.state.groups,doc.data()],
})
})
})
}
我已经从Firestore中阅读了一份文档。但是,此文档包含一个子集合(会话),其中包含许多文档。有没有一种方法可以读取包含其子集合和子文档的文档?并将它们存储在状态中? 目前,我只能看到已读文档的元素,而没有其子集合和子文档。 谢谢
答案 0 :(得分:1)
是否可以读取带有子集合和子文档的文档?
单个查询是不可能的。您将不得不分别查询每个子集合。
Firestore查询始终很浅,并且不考虑单个集合之外的文档。一般来说,模式是这样的:
dbh.collection('Groups').get()
.then(querySnapshot => {
querySnapshot.docs.map(snapshot => {
return dbh
.collection('Groups')
.doc(snapshot.id)
.collection('whatever')
.get()
})
})
.then(results => {
this.setState(...)
})
您会看到必须使用promise将查询链接在一起,然后在所有内容可用后设置状态。
答案 1 :(得分:0)
您可以使用以下查询语法访问文档的子集合:var dataSource = new kendo.data.DataSource({
data: ...,
sort: { field: "field_name", dir: "asc" }
});
asc= new kendo.data.HierarchicalDataSource({
sort: { field: "field_name", dir: "asc" },
是您的文档,dbh.collection('Groups/{groupId}/users')
是您的子集合。
然后您可以将其存储在该组的状态中。