简体   繁体   English

在 angular 中实现 rxjs switchmap

[英]Implementing rxjs switchmap in angular

This is older implementation and I wanted to get HeatingDesired value这是较旧的实现,我想获得 HeatingDesired 值

//1st api call
this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', this.refs.roomRef).subscribe(({ rows }) => {
       if (rows.length > 0) {
           rows.forEach((row) => {
               let pointId = this.helperService.stripHaystackTypeMapping(row['id']).split(' ')[0];
               //2nd api call
               this.siteService.getHisPointData(pointId, 'current').subscribe(({ rows, meta }) => {
                   let metaVal = meta;
                   if (rows.length > 0) {
                       let HeatingDesired = this.helperService.stripHaystackTypeMapping(rows[0].val);

                   }
               });
           });

       }

   });

========================================================================== ==================================================== =========================

Now I am trying to use switchmap and subscribe to the final response现在我正在尝试使用 switchmap 并订阅最终响应

getDesiredHeatingTemp(eleId){
    //1st api call
    return this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', eleId).pipe(
  switchMap((a)=>{
      a.forEach((m) => {
        let pointId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
        //2nd api call
        return this.siteService.getHisPointData(pointId, 'current')
      })
  }))
}

this.getDesiredHeatingTemp(eleId).subscribe((a) => console.log(a))

But is gives me error saying
Argument of type '(a: any) => void' is not assignable to parameter of type '(value: any, index: number) => ObservableInput<any>'.
Type 'void' is not assignable to type 'ObservableInput<any>'.

The 2nd api call expects parameter which I get from 1st api call第二个 api 调用需要我从第一个 api 调用中获得的参数

Update更新

The response which I get from 1st api call looks like

a=
cols: (23) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
meta: {ver: "2.0"}
rows: [{…}]

a=
 cols: (23) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
 meta: {ver: "2.0"}
 rows: Array(1)
      0:
       air: "m:"
       desired: "m:"
       id: "r:5d90685e647845530d95ad0a nizam346_2-VAV-9600-desiredTempHeating"
       kind: "Number"
       __proto__: Object
       length: 1
       __proto__: Array(0)

From the above response of 1st api call I want to get rows[0].id and pass it on to the 2nd api call.从第一个 api 调用的上述响应中,我想获取 rows[0].id 并将其传递给第二个 api 调用。

I tried using map, foreach but on debugging the execution doesn't go inside const obs = a.forEach((m)=>{ let pointId = this.helperService.stripHaystackTypeMapping(m.rows[0]['id']).split(' ')[0];我尝试使用 map,foreach 但在调试执行时不会 go 在 const obs = a.forEach((m)=>{ let pointId = this.helperService.stripHaystackTypeMapping(m.rows[0]['id']) .split(' ')[0];

getDesiredHeatingTemp(eleId){
    return this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', eleId)
    .pipe(
    switchMap(a => {
      const obs = a.forEach((m)=>{
        let pointId = this.helperService.stripHaystackTypeMapping(m.rows[0]['id']).split(' ')[0];
        return this.siteService.getHisPointData(pointId, 'current')
      })
      return forkJoin(obs)
  }))
}

swithmap must return an observable swithmap 必须返回一个 observable

getDesiredHeatingTemp(eleId){
    //1st api call
    return this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', eleId).pipe(
  switchMap((a)=>{
     //you formed a unique observable using forkjoin
      const obs=a.map((m)=>{
        let pointId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
        return this.siteService.getHisPointData(pointId, 'current')
      })
      //in obj we has,e.g. [
      //   this.siteService.getHisPointData(1,'current'),
      //   this.siteService.getHisPointData(1,'current'),
      //   this.siteService.getHisPointData(1,'current')
      //  ]
      return forkJoin(obs)
  }))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM