我们假设你有以下结构:
shopping-carts (collection)
- shopping-cart 1(doc)
-- dateCreated (field)
-- items (collection)
- shopping-cart 2(doc
-- dateCreated
-- items
.
.
.
那么,我们如何将整个购物车(doc)作为我们定义的ShoppingCart对象
export interface ShoppingCart {
items: ShoppingCartItem[]
dateCreated: string
}
afs.doc('shopping-cart/id').valueChanges()
仅返回dateCreated
afs.doc('shopping-cart/id').collection('items').valueChanges()
会返回项目。
简单地将所有内容合并为一个并将其创建为对象?
答案 0 :(得分:0)
可能不是最好的解决方案,但还无法找到更好的解决方案。
this.shoppingCartsCollection = afs.collection<Servicio>('shopping-carts',
ref => ref.orderBy('dateCreated', 'asc'));
this.shoppingCarts = this.shoppingCartsCollection
.snapshotChanges()
.map(values => {
return values.map(value => {
const shopping-cart = value.payload.doc.data();
const itemsArray = shopping-cart.items;
shopping-cart.observableItems = [];
for (var i = 0, len = itemsArray.length; i < len; i++) {
itemDocRef = itemsArray[i];
value.observableItems.push(afs.doc('items/'+ itemDocRef.id).valueChanges());
}
return shopping-cart;
});
});
访问您的商品数据,就像任何其他可观察的
一样<div *ngFor="let shoppingCart of shoppingCarts | async">
<p *ngFor="let observableItem of shoppingCart.observableItems">
{{(observableItem | async)?.displayName}}
</p>
</div>