从post api调用获取响应后,将引发错误=> .pipe不是函数 [英] After getting the response from post api call , an error is thrown => .pipe is not a function

查看:145
本文介绍了从post api调用获取响应后,将引发错误=> .pipe不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行api调用,并期望可以将其传递给第二个api调用,但出现错误

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

错误TypeError:this.helperService.getPointIdbyTags(...)[0] .pipe不是函数

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

在.pipe(switchMap((resarray:any)=> {

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

TS代码

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

然后我尝试将其传递给

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

推荐答案

getPointIdbyTags(buildingObj: any, zoneTags: any, roomRef: string = undefined) { ... }似乎是一个简单的函数,它返回一个数组.因此,无需使用.pipe().switchMap运算符,因为使用RXJS可以更轻松地编写异步或基于回调的代码.

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.

您的代码应如下所示:

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

}

这篇关于从post api调用获取响应后,将引发错误=> .pipe不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆