[英]How to fetch nested json data from firebase?
I want to create a dropdown menu from data that is coming from realtime database.我想根据来自实时数据库的数据创建一个下拉菜单。 I have written few lines of code but not able to get subcategory created an interface
我写了几行代码但无法让子类别创建接口
export interface MenuInfo {
name: string;
subcategory?: MenuInfo[];
}
menuList: MenuInfo[] = [];
constructor(public dataService: DataService) {}
ngOnInit() {
this.getMenuList();
}
getMenuList() {
this.dataService.getMenuList().subscribe(data => {
this.menuList = this.dataService.snapShotToArray(data);
console.log("menulist"+ this.menuList[0].subcategory);
});
}
getMenuList() {
return this.fdb.list('category').snapshotChanges();
}
snapShotToArray(snapShot) {
let retArr: any[] = [];
snapShot.forEach(element => {
let obj: any = {};
obj = element.payload.val();
obj.key = element.key;
retArr.push(obj);
});
return retArr;
}
This is json data from firebase.这是来自 firebase 的 json 数据。
category
-LtygKwodI5HdptwuInj
name: "Helmet"
subcategory
-LtygbtNUgCPC4paDswy
name: "full-face"
After retrieving the key
add a reference to node subcategory
, subscribe to the observable and then using forEach you can access the value of name
inside subcategory:检索
key
后添加对节点subcategory
的引用,订阅可观察对象,然后使用 forEach 可以访问子类别内name
的值:
snapShot.forEach(element => {
let lists = this.fdb.list("category/"+element.key+"/"+"subcategory").snapshotChanges();
lists.subscribe(data => {
data.forEach(subElem => {
console.log(subElem);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.