1

我坚持这一点:我的数据库非常简单。

 {
      "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')

在这种情况下,它返回这些参考:-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>

我收到此错误:

原始异常:TypeError:未定义不是对象(评估'self.parent.context.ref.shortdesc')

我需要你的帮助 !

4

1 回答 1

4

正确的地图是这样的。最后我找到了解决方案,希望对其他人有所帮助。

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>
于 2016-08-03T23:49:42.730 回答