2016-08-03 15 views
1

我被困在這個: 我的數據庫非常簡單。ionic2中的angularfire2,如何映射?

{ 
     "cities" : { 
     "A" : true, 
     "B" : true, 
     "C" : true, 
     }, 
     "productspercities" : { 
     "A" : { 
      "-KNnf89UzBvzmYlrpiQj" : true, 
      "-KNnfAs31LkMUjU60VAG" : true, 
      "-KNngYcNoUUql4oIMxwU" : true 
     }, 
     "B" : { 
      "-KO-wEN536BAx15ZpTO1" : true, 
      "-KO-xsw5Af1QNqTzDo2K" : true 
     }, 
     "C" : { 
      "-KNvvTEN5B0jbPLfOHxr" : true, 
      "-KO-v_WR8LSk4UfnGoYy" : true, 
      "-KO-viF1tOSp4JmpQxdi" : true 
     } 
     }, 
     "short_desc" : { 
     "-KNnf89UzBvzmYlrpiQj" : { 
      "desc" : "A nice flat in A" 
     }, 
     "-KNnfAs31LkMUjU60VAG" : { 
      "desc" : "A nice land in A" 
     }, 
     "-KNngYcNoUUql4oIMxwU" : { 
      "desc" : "A nice home in A" 
     }, 
     "-KNvvTEN5B0jbPLfOHxr" : { 
      "desc" : "A nice flat in C" 
     }, 
     "-KO-v_WR8LSk4UfnGoYy" : { 
      "desc" : "A nice land in C" 
     }, 
     "-KO-viF1tOSp4JmpQxdi" : { 
      "desc" : "A nice home in C" 
     }, 
     "-KO-wEN536BAx15ZpTO1" : { 
      "desc" : "A nice home in B" 
     }, 
     "-KO-xsw5Af1QNqTzDo2K" : { 
      "desc" : "A nice land in B" 
     } 
     } 
    } 

說我想只顯示產品,爲城市A,所以我用這個函數來獲取產品的所有裁判對這個城市:

this.productspercities = this.af.database.list('/productspercities/A') 

在這種CAS,它返回這些裁判: -KNnf89UzBvzmYlrpiQj -KNnfAs31LkMUjU60VAG -KNngYcNoUUql4oIMxwU

所以,現在我所知道的產品,以顯示有這些裁判。但如何獲得這些產品的詳細信息(short_desc)?

我嘗試這樣做:

getShortDesc(city) { 
     this.productspercities = this.af.database.list('/productspercities/'+city) 
     .map((productspercities) => { 
     return productspercities.map((ref) => 
     { 
      ref.shortdesc = this.af.database.list('/short_desc/${ref.$key}') 
     return ref; 
     }) 
     }) 

應用程序是正確的建設沒有任何警告,但是當我嘗試以顯示與視圖結果:

<ion-card *ngFor="let description of ref.shortdesc | async"> 
<ion-item> 
<h2>{{description.desc}}</h2> 
</ion-item> 
</ion-card> 

我得到這個錯誤:

ORIGINAL EXCEPTION: TypeError: undefined is not an object (evaluating 'self.parent.context.ref.shortdesc')

我需要你的幫助!

回答

3

正確的地圖是這個.. 最後我找到了解決方案,並希望它能幫助別人。

this.productspercities = this.af.database.list('/productspercities/'+city) 
     .map((items) => { console.log(items); 
     return items.map(item => { item.short_desc = this.af.database.object('/short_desc/'+item.$key); 
     return item; }); 
     }); 

然後在視圖:

<ion-card *ngFor="let item of productspercities | async"> 
<ion-item> 
<h2>{{(item.short_desc | async)?.desc}}</h2> 
</ion-item> 
</ion-card>