Firebase/Angularfire2:firebase 中的嵌套数据节点 [英] Firebase / Angularfire2: nested data nodes in firebase

查看:35
本文介绍了Firebase/Angularfire2:firebase 中的嵌套数据节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 firebase 数据库.一个名为/config//的节点,它保存用户特定的数据.我现在想向 userdata 添加一个包含汽车列表的子树,例如/config//cars/:

i've got a firebase database. a node called /config// which holds userspecific data. i now want to add a subtree to the userdata with a list of cars, like /config//cars/:

"config" : {
    "NE5AwHcxKofMPKRCAvny5Re4MtG3" : {
      "geburtstag" : "2017-06-04",
      "nachname" : Mueller",
      "cars" : {
        "-Krr2-DC1uoqCvgVJhFL" : {
          "name" : "Ford Mustang"
        },
        "-Krue4_5kJQhis-3Qg1X" : {
          "name" : "Toyota Supra"
        }
      }
    }

我创建了一个到/config/的可观察绑定

i created a observable binding to /config/

this.$ref = this.database.object('/config/' + uid);
this.data = this.$ref.subscribe();

我可以访问主配置节点的数据并将其与 angular 绑定,但是 Cars 子节点上的 ngFor 不起作用.我的数据结构错误还是需要某种映射?

i can access the data of the main config-node and bind it with angular, but a ngFor on the Cars subnode doesn't work. Am i structuring my data wrong or do I need some kind of mapping here?

html 部分:

<ion-item *ngFor="let car of data.cars | async">
    {{car.name}}
 </ion-item>

推荐答案

您将 data 分配给订阅,而您应该将其分配给可观察对象:

You're assigning data to your subscription while you should be assigning it to the observable:

cars: FirebaseListObservable<any[]>;
this.cars = this.database.list(`/config/${uid}/cars`);

现在您可以在 ngFor 循环中调用 async 数据:

Now you can call your async data in your ngFor loop:

<ion-item *ngFor="let car of cars | async">
  {{ car.name }}
</ion-item>

这篇关于Firebase/Angularfire2:firebase 中的嵌套数据节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆