Javascript 存储firebase数据库中的数组
我正试图用角度和角度火力来建造一个项目。我的项目是使用传单地图绘制多边形,然后将多边形的坐标推送到数据库。我已成功地将数组推送到数据库中,但从数据库中检索坐标时遇到问题 数据库结构:Javascript 存储firebase数据库中的数组,javascript,arrays,angular,firebase-realtime-database,angularfire2,Javascript,Arrays,Angular,Firebase Realtime Database,Angularfire2,我正试图用角度和角度火力来建造一个项目。我的项目是使用传单地图绘制多边形,然后将多边形的坐标推送到数据库。我已成功地将数组推送到数据库中,但从数据库中检索坐标时遇到问题 数据库结构: { "Locations" : { "-M0M9kqEbE0FVMzIZAg0" : { "text" : [ [ { "lat" : 35.97800618085566, "lng" : 42.03369140625001 }, {
{
"Locations" : {
"-M0M9kqEbE0FVMzIZAg0" : {
"text" : [ [ {
"lat" : 35.97800618085566,
"lng" : 42.03369140625001
}, {
"lat" : 33.88865750124075,
"lng" : 41.46240234375001
} ] ]
}
}
}
我试图将上面的数组存储在类ts中,而不是html中
我的代码:
export class WeatherNowComponent {
itemsRef: AngularFireList<any>;
items: Observable<any[]>;
coords :any
constructor(private db: AngularFireDatabase) {
this.itemsRef = this.db.list('Locations')
// Use snapshotChanges().map() to store the key
this.items = this.itemsRef.snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
);
this.db.list('Locations').snapshotChanges().subscribe((markers: any) => {
console.log(markers)
markers.forEach(singlemarker => {
console.log(singlemarker)
// ADD COORDS to MAP POLYGON
var polygon = L.polygon([singlemarker.text.lat,singlemarker.text.lng], {color: 'red'}).addTo(this.map);
});
});
console.log(this.items)
}
}
导出类组件{
itemsRef:AngularFireList如果您因为是数组而故意调用扩展运算符,请尝试将其指定给对象文字定义中的属性值
{ key: c.payload.key, values: ...c.payload.val() }
编辑:根据您的评论,我仍然认为只需将坐标数组(lat/lng属性对对象)分配给values属性,以便稍后迭代。我假设.val()方法必须返回这些值的数组。否则,请发布.val()返回的内容
另外,我还注意到一些你应该检查的东西
observable正在调用RxJs pipe()方法来映射,但我没有注意到任何地方都有subscribe()。而.pipe()允许映射和过滤回调函数,因为它需要一个subscribe
编辑#2:根据你下面的第二条评论。我还不清楚你想要什么。因此,我将根据评论再给出两个可能的答案:“好的,我的问题是如何直接获取数组?”
编辑#3:哦,是的,我明白了。数组在添加到它之前需要初始化
更新的代码。此外,作为对您未来问题的额外帮助,如果您有一个snippet.S.O.问题可供参考,这将非常有帮助:
希望这能有所帮助
快乐编码!“位置”:{
应该是“位置”:[{
?这是我在控制台日志中的内容,非常感谢您的解释。但我有一个错误是错误类型错误:无法读取//下未定义的属性“推送”
,检索位置并存储坐标以直接获取阵列数据
// Possibility #1
// Simplest is to store off the returned data into the coords variable for later processing.
// You can then get the array directly at any later point just by accessing the coords
this.db.list('Locations').snapshotChanges().subscribe((markers: any) => {
coords = markers;
});
// Possibility #2
// Since the data structure you are using works like a dictionary data structure
// Define some structures to store off the data and get at the array data directly later
// Define this somewhere outside your class
interface ICoordinate {
lat: string;
lng: string;
}
// Initialize coordinate dictionary
let coordinates: { [key: string] : ICoordinate[]; } = {};
// Retrieve locations and store off coordinates to get at the array data directly
this.db.list('Locations')
.snapshotChanges()
.subscribe((markers: any) => {
coordinates[markers.key] = ICoordinate[];
markers.forEach(singlemarker => {
coordinates[markers.key].push({ lat: singlemarker.text.lat, lng: singlemarker.text.lng });
});
});