To assign value to a variable on subscribing to th

2020-02-15 06:29发布

问题:

I want to assign the response value to ahumode variable in order to access the value thoughout the class and after assigning , call this.loadFloorLayouts() function

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 is undefined

If I try something like this

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()

I get an observable object.

I have checked the 2nd api call also executes but not able to assign the value to the variable

Update

If I only subscribe to 1st api call

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

This is the response I get

{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"

I want to get id from this response and make the 2nd api call using the id

Update2

I tried sometjing as below but the execution doesnt go inside pipe(switchmap and no errors

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;  
            })
    )

回答1:

I put explanations in the comments.

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);
})


回答2:

Please try below code:

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);
})