我是Angular和RxJS的新手,并一直在努力解决以下问题。
我具有以下firebase实时数据库结构:
-States
- AZ
-Flagstaff
-ar
-0
-name: "Default"
-amount: 100
...
-1
-name: "Alt 1"
-amount: 200
...
-Phoenix
-ar
-0
-name: "Default"
-amount: 150
-1
-name: "Alt 1"
-amount: 200
- CA
-Anaheim_Hills
...
我正在尝试构建一个Angular服务,该服务将拉动State节点(例如AZ)的引用,以遍历对象树,选择某些项,进行一些操作并返回以下对象的简单数组:
interface Jurisdiction {
path: string; //eg '/States/AZ/Flagstaff'
name: string;
categories: [{
path: string; //eg '/States/AZ/Flagstaff/ar/0'
name: string; // eg 'Default'
}];
}
根据我在网上发现的内容,我整理了以下方法,但看来take(1)
不再是可观察的方法。我也不确定如何在方法返回Jurisdiction []
数组之前确保异步遍历功能完成。
getJurisdictions(state: string): Jurisdiction[] {
let ret: Jurisdiction[];
const statePath = '/States/' + state;
this.stateRef = this.db.list(statePath);
const s = this.stateRef.snapshotChanges().take(1).toPromise().then(snaps => {
snaps.forEach(snap => {
let jur: Jurisdiction;
jur.path = statePath + '/' + snap.key;
jur.name = snap.key.replace('_', ' ');
snap.payload.forEach(snap2 => {
let cat: {
path: string;
name: string;
};
cat.path = jur.path + '/' + 'SUB PATH PLACEHOLDER eg Flagstaff/ar/0';
cat.name = 'NAME PLACEHOLDER';
jur.categories.push(cat);
return true;
});
ret.push(jur);
});
});
return ret;
}