正如另一個問題所述,我正在開發一個使用Firebase作爲後端的Ionic 2應用程序。AngularFire2:使用RxJS .map()執行FirebaseListObservables上的'連接'
我有類別,我有產品。產品屬於類別。由於它是「從n到m」的關係,因此產品和類別存儲在Firebase的不同節點中。我構造的數據如下:
類別知道,哪些產品屬於他們(按鍵在每個類別中的「刺針」節點引用)。產品知道它們屬於哪個類別(密鑰在「prod_cat」節點中引用)。 但是,當我列出所有類別時,我只知道屬於該類別的產品的ID。在模板中,我需要顯示更多詳細信息,例如產品名稱。
我看過不少類似的問題的想出了這個解決方案的產品信息添加到類:
getProductsOfCategory(catId){
this.productsOfCategory = this.af.database.list('/categories/'+catId+'/prods');
return this.productsOfCategory
.map(products => {
console.log(products); // works fine: an object containing the relevant product information is logged to the console (see screenshot)
products.map(product => { console.log(product.$key); // works fine: the IDs of the products are logged to the console
product.prod_details = this.af.database.object('/products/'+product.$key); // seems not to work. Returned value: undefined
});
});
不幸的是,這是行不通的。 作爲寫爲代碼中的註釋,所述產品信息被正確地收集並記錄到控制檯(見下圖): console screenshot
然而,上述功能的返回的對象是「未定義」。
當我嘗試狀態,類型FirebaseListObservable的是明確地定義一個目的是要返回,我收到錯誤:
Type 'Observable' is not assignable to type 'FirebaseListObservable'. Property '$ref' is missing in type 'Observable'.
沒有任何人有一個想法,還有什麼我可以嘗試?
非常感謝您提前!