【问题标题】:To assign value to a variable on subscribing to the response of an Observable在订阅 Observable 的响应时为变量赋值
【发布时间】:2020-02-02 00:37:55
【问题描述】:

我想将响应值分配给 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 调用

更新2

我尝试了下面的 sometjing,但执行没有进入管道(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;  
            })
    )

【问题讨论】:

  • 所以您想将来自siteService.getPointData 的响应分配给ahumode 变量?
  • 是的,siteService.getHisPointData(ahuId,"current") 到 ahumode
  • rows.rows 是一个数组,并且这个数组中的 foreach 项目你需要点击这个方法并从所有来源获得响应(forkjoin 可以在这里提供帮助)并推送到ahumode
  • 你能举个例子,我需要更正吗

标签: angular observable


【解决方案1】:

我在 cmets 中放了解释。

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

【讨论】:

  • 您好,如何取消订阅 forkjoin 呼叫,谢谢
  • 取消订阅一个订阅,您将取消订阅所有流。
【解决方案2】:

请尝试以下代码:

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

【讨论】:

  • 我还没有尝试过你的解决方案,但你已经添加了一个订阅,只是想了解它会有什么不同,因为我已经提到它是自定义函数并且不会返回我一个 observabie ,striphaystackmapping 只是简单地拆分并返回我的 id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2012-05-19
  • 2017-08-04
  • 2013-06-24
  • 1970-01-01
  • 2019-07-13
  • 2019-05-26
相关资源
最近更新 更多