我正在寻找通过Angular服务将Firebase实时数据库中的对象映射到对象数组的正确方法。给定以下对象。
目前,我尝试使用AngularFireMap
以及类型为array的Observable
。
这是标记对象的接口。
export interface IGeometry {
type: string;
coordinates: number[];
}
export interface IGeoJson {
type: string;
geometry: IGeometry;
properties?: any;
$key?: string;
}
export class GeoJson implements IGeoJson {
type = 'Feature';
geometry: IGeometry;
constructor(coordinates, public properties?) {
this.geometry = {
type: 'Point',
coordinates
};
}
}
export class FeatureCollection {
type = 'FeatureCollection';
constructor(public features: Array<GeoJson>) {}
}
当前,我尝试通过.valueChanges()
将从Firebase实时数据库接收到的对象映射到标记对象,并且效果很好,但是我需要数据库中的$ key来标识标记,但是如果我想工作实际上,使用.snapshotChanges()
不能很好地工作,因为我无法以正确的方式映射对象以接收对象数组。
这是我当前尝试以正确的方式接收服务中的所有地图标记的功能。
public getMarkers(): Observable<GeoJson[]> {
return this.db.list('/markers')
.snapshotChanges()
.pipe(
map(data => {
let geoData: GeoJson[];
data.forEach( singleData => {
// @ts-ignore
geoData.push(singleData.payload.val());
});
return geoData;
}));
}