将计算字段与angularfire2列表组合

时间:2018-03-13 09:38:36

标签: rxjs angularfire2

我尝试将新创建的字段组合到angularfire2列表中,但是实现maping失败了,

echo

我期待像data.distance这样的结果,所以我可以调用数据和距离将包括。

我想弄清楚如何在angularfire2列表中实现这个

 const listLoc = this.afdb.list('/userLocations');
listLoc.snapshotChanges()
  .subscribe(actions => {
    actions.forEach(action => {
      let data = action.payload.val();
      let placeLocation = {
        lat: data.lat,
        lng: data.lng
      }
      let usersLocation = {
        lat: -24.25747,
        lng: 112.752
      };
      let distance = this.getDistanceBetweenPoints(
        usersLocation,
        placeLocation,
        'km'
      ).toFixed(2);
      console.log('action payload.val()', data, distance);
    });
  });

请指导,谢谢。

1 个答案:

答案 0 :(得分:0)

哦,我可以在snapshotChanges()

之后映射
 const listLoc = this.afdb.list('/userLocations');
listLoc.snapshotChanges()
  .map(actions => {
    actions.map(action => {
      let data = action.payload.val();
      let placeLocation = {
        lat: data.lat,
        lng: data.lng
      }
      let usersLocation = {
        lat: -7.25747,
        lng: 112.752
      };
       data.distance = this.getDistanceBetweenPoints(
        usersLocation,
        placeLocation,
        'km'
      ).toFixed(2);
      console.log('the result i expect', data);
    });
  }).subscribe();