【问题标题】:Angular Firebase How to iterate through a list object and take dataAngular Firebase如何遍历列表对象并获取数据
【发布时间】:2017-10-24 09:06:33
【问题描述】:

我正在尝试从数据库中提取位置数据,彻底迭代并在地图上显示标记。 现在我用这个拉数据:

this.msgData =this.db.list(`/messages/`).valueChanges();
    this.msgData.take(1).subscribe(msg =>{

console.log(msg);



});

输出:

如您所见,位置数据是这样存储的。有 authid-messageid,然后是 locationlat 和 locationlong 数据。

我想到的第一个想法是从 db 中提取所有 locationlat locationlong 以某种方式将它们存储在一个数组中,然后使用 for 循环将它们添加到地图中,但我真的不知道。如何在 db 中提取每个 msg 的 locationlat 和 locationlong ?

谢谢

【问题讨论】:

  • 你能把结果显示在json里吗
  • 其实我也在尝试提取味精和用户名以便在每个标记上写字
  • 你能说得更具体点吗?你说你想拉 lat/lng 信息然后你表明你的请求已经返回了 lat/lng 信息,那么到底是什么问题?

标签: angular firebase leaflet angularfire


【解决方案1】:

编辑:Demo

问题是我们不知道数组中每个对象的键名,所以试试这样的方法:

function getObjectWithoutKnowingKey(data) {
    let objects = [];
    for (var propName in data) {
        if (data.hasOwnProperty(propName)) {
            objects.push(data[propName]);
        }
    }
    return objects;
}

在你的功能中:

this.msgData =this.db.list(`/messages/`).valueChanges();
    this.msgData.take(1).subscribe(msg =>{

       let data = msg.map(this.getObjectWithoutKnowingKey)

       console.log(data); // [{ locationlat: 1, locationlong: 2, message: 'nsg', username: 'john' }]
    });

【讨论】:

  • 嗨,谢谢,现在我可以使用 console.log(data[0].locationlat);但这只会提取每个身份验证 ID 的 1 条第一条消息。其他消息呢? image.prntscr.com/image/HQPaqFW5SpyKqy0f9399Gw.png
  • 我不明白你想说什么。请编辑您的问题并阐明您想要通过身份验证 ID 表示的含义并添加更多 json。
  • @YunusEmreGuneyStudent 看到更新我改变了getObjectWithoutKnowingKey函数
猜你喜欢
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多