一次订阅父母收集和子女收集
将父馆集合表
带有文档,每个文档都有一个名为orders
的集合。我想订阅表并收听订单更改。我尝试的是:
在我的桌子服务上
watchTables(): Observable<any> {
return this.afStore
.collection('restaurants')
.doc(this.rid)
.collection('tables')
.snapshotChanges()
.pipe(
map((data: any) =>
data.map((documentDataTable: any) => ({
id: documentDataTable.payload.doc.id,
...documentDataTable.payload.doc.data()
}))
.sort((a: any, b: any) => a.number - b.number)
),
mergeMap((tables: any) => this.ordersService.watchOrders(tables))
);
}
,我的订单服务
watchOrders(tables: any): Observable<any> {
let orders$: Observable<any>[] = [];
tables.forEach((table: any) =>
orders$.push(
this.afStore
.collection('restaurants')
.doc(this.rid)
.collection('tables')
.doc(table.id)
.collection('orders')
.snapshotChanges()
.pipe(
map((data: any) => {
return data.map((documentDataOrder: any) => ({
id: documentDataOrder.payload.doc.id,
...documentDataOrder.payload.doc.data()
}))
.sort((a: any, b: any) => this.sortOrders(a, b))
})
).pipe(
map(data => ({ table: table, orders: data }))
)
));
return zip(orders$);
}
在我的组件上消耗餐桌服务,
subscribeToTables() {
this.tablesService
.watchTables()
.subscribe((collectedTablesData: any) => {
console.log('table or orders changed');
});
}
我对数据造成数据的影响,但我自己找不到答案。或者我只是在弄乱可观察到的东西,可能两者兼而有之。
我需要在解决方案中需要的东西:
- 我需要
订单
数据同时我获得表
data(这就是为什么我首先认为订单将成为表Doc的一部分地图数组,但不是过分吗 - ?代码>状态。
Having a parent collection tables
with documents which each one have a collection named orders
. I want to subscribe to tables and listen to orders changes. What I tried:
At my tables service
watchTables(): Observable<any> {
return this.afStore
.collection('restaurants')
.doc(this.rid)
.collection('tables')
.snapshotChanges()
.pipe(
map((data: any) =>
data.map((documentDataTable: any) => ({
id: documentDataTable.payload.doc.id,
...documentDataTable.payload.doc.data()
}))
.sort((a: any, b: any) => a.number - b.number)
),
mergeMap((tables: any) => this.ordersService.watchOrders(tables))
);
}
At my orders service
watchOrders(tables: any): Observable<any> {
let orders$: Observable<any>[] = [];
tables.forEach((table: any) =>
orders$.push(
this.afStore
.collection('restaurants')
.doc(this.rid)
.collection('tables')
.doc(table.id)
.collection('orders')
.snapshotChanges()
.pipe(
map((data: any) => {
return data.map((documentDataOrder: any) => ({
id: documentDataOrder.payload.doc.id,
...documentDataOrder.payload.doc.data()
}))
.sort((a: any, b: any) => this.sortOrders(a, b))
})
).pipe(
map(data => ({ table: table, orders: data }))
)
));
return zip(orders$);
}
Consuming tables service at my component
subscribeToTables() {
this.tablesService
.watchTables()
.subscribe((collectedTablesData: any) => {
console.log('table or orders changed');
});
}
I thougth about desestructuring data, but couldn't find an answer myself. Or I'm just messing with Observables, probably both.
Things I need in the solution:
- I need
orders
data at the same time I gettables
data(thats why in first place I thought orders would be part of table doc as an array of maps, but isn't that overkill?) - I need to know when orders change at my Table component because contains a rowexpansion datatable,
table
state in the datatable is dependant oforders
state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎已经聚集在一起,无法一次听父母文件及其子女收藏。 Firestore的阅读和听力很浅,仅涵盖一个具有一定名称的收藏或所有收藏。
您有一些选择:
您可以在表格文档中包含一个字段,该字段指示其订单状态。最简单的是,这可能是
orderslastupdatedat
timestamp字段,但是您还可以列出订单ID的列表和他们的个人最近更新的时间戳。无论哪种方式,您都应在更新相关顺序时更新表文档中的字段,然后将其用作客户端中的触发器以(重新)加载表的订单。您还可以使用 on
订单
在表文档的路径上,如Sam在回答 CollectionGroupQuery中的答案中所示,但限制搜索特定文档下的子策略。As you seem to have gather already, there is no way to listen to parent documents and their child collections at once. Reads and listens in Firestore are shallow, and only cover one collection or all collections with a certain name.
You have a few options:
You can include a field in the table document that indicates the state of its orders. At its simplest this could be a
ordersLastUpdatedAt
timestamp field, but you could also have a list of order IDs and the timestamp of their individual most recently updates. Either way, you should update the field(s) in the table document whenever a relevant order is updated too, and then use that as a trigger in your client to (re)load the orders for the table.You can also use a collection group query on
orders
at the path of the table document, as Sam showed in his answer to CollectionGroupQuery but limit search to subcollections under a particular document.