After getting the response from post api call , an error is thrown => .pipe is not a function

只愿长相守 提交于 2019-12-13 07:57:29

问题


I am making an api call and expecting a response from it which can be passed to 2nd api call but I am getting an error

ERROR TypeError: this.helperService.getPointIdbyTags(...)[0].pipe is not a function

on line .pipe(switchMap((resarray:any)=>{

TS code

someFunction(floor){
 floor.entities.forEach(element => {
       let desiredTempForZone;

       this.getCurrentValue(element.name).subscribe((des) => 
                      {
                         currentTempForZone = des
                       });
       console.log(currentTempForZone);
})
}

getCurrentValue(eleName){
    let roomObj = this.getRoomObj(eleName);
    let equipRef = roomObj.map(equip => equip.entities.filter(entity => entity.entities.length > 0)[0])[0];

    return this.helperService.getPointIdbyTags(this.buildings, ['current', 
             'temp'], equipRef.referenceIDs.room)[0]
              .pipe(switchMap((resarray:any)=>{
                   const res = resarray[0]
                   return  this.siteService.getHisPointData(res, 'current')
                       .pipe(
                           map(this.helperService.stripHaystackTypeMapping),
                       )
              }));
}

And then I am trying to pass it on to

switchMap((resarray:any)=>{
               const res = resarray[0]
               return  this.siteService.getHisPointData(res, 'current')

回答1:


It looks like that getPointIdbyTags(buildingObj: any, zoneTags: any, roomRef: string = undefined) { ... } is a simple function which returns an array. So there is no need to use .pipe() and .switchMap operators because RXJS is used to make it easier to compose asynchronous or callback-based code.

Your code should look like this:

getCurrentValue(eleName){
    let roomObj = this.getRoomObj(eleName);
    let equipRef = roomObj.map(equip => equip.entities
        .filter(entity => entity.entities.length > 0)[0])[0];

    let res = this.helperService.getPointIdbyTags(this.buildings, ['current', 
             'temp'], equipRef.referenceIDs.room)[0];

    // If this method `getHisPointData()` really makes HTTP call
    // and if it returns observable than you can use `pipe` operator
    return  this.siteService.getHisPointData(res, 'current')
                       .pipe(
                           map(this.helperService.stripHaystackTypeMapping),
                       )
                       .subscribe(s => console.log(s));

}


来源:https://stackoverflow.com/questions/58172059/after-getting-the-response-from-post-api-call-an-error-is-thrown-pipe-is-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!