更新本地变量,云存储中的每个数据更改

时间:2019-05-19 12:47:24

标签: angular typescript firebase rxjs google-cloud-firestore

每次在Firefire中存储数据时,我都无法设置局部变量。例如,在局部变量调用count_vehicle中,该值是基于Cloud Firestore中数据的具有特殊条件的值的计数。例如,运行程序count_vehicle = 4之后,当我更改Firestore中的数据时,经过一些计算,count_vehicle变量必须为3,而改为count_vehicle become 7 (3+4)。为了解决这个问题,我的意图是在开始计算之前设置count vehicle = 0。这是我的代码

dashboard.component.ts

//import section

//component declaration section



export class DashboardComponent implements OnInit {
  items = any[];
  count_vehicle: number =0;

  constructor(public service: FireService) {
  }
  startAnimationForLineChart(chart){
  ngOnInit() {
      this.service.getVehicle().pipe(     
        map(actions => actions.map(a => {       
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          if(data.value>300){
            this.count_vehicle++;
          }
          return {id, .. data};
        }))
      ) 
      .subscribe(res => {
        this.items = res;       
        console.log(res)
      })

      }
}

fire.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class FireService {

  constructor(public db : AngularFirestore) { }

  getVehicle() : Observable<any[]>{
    return this.db.collection('/recent_sensor_data').snapshotChanges()
    }
    }
  }

谢谢您的帮助。对不起我的英语

1 个答案:

答案 0 :(得分:0)

您应该可以添加一个值,以将计数器重置为可观察值,例如:

this.service.getVehicle().merge(Rx.Observable.from([ -1*this.count_vehicle ])).pipe(...