在订阅Observable的响应时为变量赋值

时间:2019-10-03 16:35:20

标签: angular observable

我想将响应值分配给ahumode变量,以便在整个类中访问该值,并在分配后调用this.loadFloorLayouts()函数

ahumode = [];

//1st async api
this.siteService.getParamsEquip(this.buildings.ccus[0].referenceIDs.ahu,"system")
.pipe(switchMap( rows =>{
    return rows.rows.map((m) => {
    // this is a custom function
    let ahuId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
    console.log(ahuId)
    //2nd async api
    this.siteService.getPointData(ahuId,"current")  
  })  
})
).subscribe(a => {
   this.ahumode = a[0].val;
   this.loadFloorLayouts(this.buildings.floors);
})

a是undefined

如果我尝试这样的事情

ahumode = [];
this.siteService.getParamsEquip(this.buildings.ccus[0].referenceIDs.ahu,"system")
.pipe(switchMap( rows =>{
    return rows.rows.map((m) => {
    let ahuId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
    this.ahumode.push(this.siteService.getHisPointData(ahuId,"current"))
    //this.siteService.getPointData(ahuId,"current") 
    this.loadFloorLayouts(this.buildings.floors) 
  })  
})
).subscribe()

我得到一个可观察的物体。

我检查了第二个api调用是否也执行了,但无法将值赋给变量

更新

如果我只订阅第一个api调用

 this.siteService.getZoneParamsByEquip
  (this.buildings.ccus[0].referenceIDs.ahu,"system").subscribe( r => console.log(r))

这是我得到的答复

{meta: {…}, cols: Array(15), rows: Array(1)}
cols: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
meta: {ver: "2.0"}
rows: Array(1)
  0:
  dis: "Dd_350.1-SystemEquip-operatingMode"
  his: "m:"
  id: "r:5d96e1a12d9dd301026a5654"

我想从此响应中获取ID并使用ID进行第二次api调用

Update2

我尝试了如下方法,但是执行并没有进入管道内部(switchmap和没有错误

this.siteService.getZoneParamsByEquip(
    this.buildings.ccus[0].referenceIDs.ahu,"system")
        .pipe(switchMap( rows =>{
              let responses = [];
              rows.rows.map( (m) => {
              let ahuId = this.helperService
                              .stripHaystackTypeMapping(m['id']).split(' ')[0];

              responses.push(this.siteService.getHisPointData(ahuId,"current"))
              })

              forkJoin(responses).subscribe(([results]) => {
                 //check for the results here and assign them to your variable
                 this.ahumode = results
                 this.loadFloorLayouts(this.buildings.floors);
              })
              return this.ahumode;  
            })
    )

2 个答案:

答案 0 :(得分:2)

我在评论中加上了解释。

ahumode = [];

//1st async api
this.siteService.getParamsEquip(this.buildings.ccus[0].referenceIDs.ahu, "system")
   .pipe(
      switchMap(result =>
         // switchMap expects a function that returns a stream, if you're making several API calls,
         // you need to combine them with forkJoin or combineLatest
         forkJoin(
            // here you'll need an array of streams
            result.rows.data.map(m => {

               let ahuId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
               console.log(ahuId)

               // the return statement was missing
               return this.siteService.getPointData(ahuId, "current")
            })
         })))
.subscribe(a => {
   this.ahumode = a[0].val;
   this.loadFloorLayouts(this.buildings.floors);
})

答案 1 :(得分:0)

请尝试以下代码:

ahumode = [];

    //1st async api
         this.siteService.getParamsEquip(this.buildings.ccus[0].referenceIDs.ahu,"system").pipe(switchMap( rows =>{
  return rows.rows.map((m) => {
    his.helperService.stripHaystackTypeMapping(m['id']).subscribe(response=>{
    let authId=response.split(' ')[0];
    console.log(ahuId)
    //2nd async api
    this.siteService.getPointData(ahuId,"current")  
})

  })  
})
).subscribe(a => {
   this.ahumode = a[0].val;
   this.loadFloorLayouts(this.buildings.floors);
})