Merge map Angular

185 Views Asked by At

Can you tell me what am I doing wrong ?

this.scheduleService.GetZones(environment.systemId)
  .pipe(
    mergeMap((zone: Zone) => {
      return this.dashboardService.GetLatestMeasurementValue(environment.systemId, zone.sensorId)
    })
    .subscribe(mois => {
      this.currentMoisture = mois;
    })
  );
}

I get this error: Property 'subscribe' does not exist on type 'OperatorFunction'

2

There are 2 best solutions below

1
On BEST ANSWER

You cannot subscribe to an operator. You need to subscribe as follows.

this.scheduleService.GetZones(environment.systemId)
    .pipe(
       mergeMap((zone: Zone) => {
          return this.dashboardService.GetLatestMeasurementValue(environment.systemId, zone.sensorId);
       })
     )
    .subscribe(mois => {
      this.currentMoisture = mois;
    })
0
On

I see we are mapping zone value to the latest measure value by this method name GetLatestMeasurementValue. If you are always interested in the latest value switchMap is a better operator

this.scheduleService.GetZones(environment.systemId)
    .pipe(
       switchMap((zone: Zone) => {
          return this.dashboardService.GetLatestMeasurementValue(environment.systemId, zone.sensorId);
       })
     )
    .subscribe(mois => {
      this.currentMoisture = mois;
    })

And if it is a single time event simple map would also do the job as below :

this.scheduleService.GetZones(environment.systemId)
    .pipe(
       map((zone: Zone) => {
          return this.dashboardService.GetLatestMeasurementValue(environment.systemId, zone.sensorId);
       })
     )
    .subscribe(mois => {
      this.currentMoisture = mois;
    })