加入2个节点与angularfire2
问题描述:
Image of my firebase加入2个节点与angularfire2
我试着去映射用户已收藏最多的类别节点检索其信息的类别。
代码为我的打字稿:
ionViewDidLoad(categorykey:string){
console.log('Hello Manage Favourite Page');
let userid = this.authData.getID();
this.favouritecategories = this.af.database.list(`/users/${userid}/favourites`)
.map(favourites => {
favourites.map(category => {
let categorykey = category.$key
console.log(categorykey);
category.favname = this.af.database.object(`/test/${categorykey}`)
});
return favourites;
});
代码为我的HTML:
<div *ngFor="let region of favouritecategories | async; let i = index;">
<ion-item (click)=" category.hidden = !category.hidden; category.visible = !category.visible" detail-none><b>{{i+1}}.{{ (category.favname| async)?.name }}</b>
<ion-icon name="arrow-down" [hidden]="category.visible" item-right></ion-icon>
<ion-icon name="arrow-up" [hidden]="!category.visible" item-right></ion-icon></ion-item>
</div>
与上面的代码,我只设法获取类别的名称,但不是地区名称。任何人有任何想法如何做?
答
您还没有实际返回修改后的地图。你要像做以下(修改最新AngularFire版本):从原来的海报的问题
let listObservable = this.af.database.list(`/users/${userid}/favourites`);
// use snapshotChanges() as this contains the keys
this.favouritecategories = listObservable.snapshotChanges()
.map(snapshots => {
// note that we return the inner .map()!
return snapshots.map(snap => {
let data = snap.val();
data.favname = this.af.database.object(`/test/${snap.key}`).valueChanges();
// note also that we return the transformed data
return data;
});
});
差异:
- 应该使用.valueChanges()来获取ngFor OPS一个有用的可观测
- $键默认情况下不再包含在数据
- 但可以使用.snapshotChanges()获得
其他一些有用的提示:
- 避免连续的数字ID(即:数组)
- 将数据写入以后将如何读取
- 避免尝试使NoSQL看起来像SQL,其中包含许多fkey引用和人为规范化。作为一般规则,只需存储您将要阅读的名称即可。
- 可以对数据进行非规范化处理。