-1

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')
Enthu
  • 512
  • 2
  • 13
  • 38
  • 1
    You seem to be missing a right paren after ,this.helperService.getPointIdbyTags(...)[0] – GreyBeardedGeek Sep 30 '19 at 16:59
  • @GreyBeardedGeek , I dont think, if that would have been the case , then it would given compilation error, but I ran the code block and got the response of the 1st api call , I have also given the response above :) – Enthu Sep 30 '19 at 17:16
  • 1
    you should copy/paste your code again, because GreyBeardedGeek is right you are definitly missing a parenthesis here.... – mickdev Sep 30 '19 at 17:36
  • ok sure, will do – Enthu Sep 30 '19 at 17:48
  • @mickdev , I have updated , please let me know , incase again the parenthesis are missing – Enthu Sep 30 '19 at 17:53
  • Ok, that's better. The issue seems to be that you are calling `pipe` on an javascript array. You can use `pipe` only only observables, try this instead : `getCurrentValue(eleName){ //.. return this.helperService.getPointIdbyTags(this.buildings, ['current', 'temp'], equipRef.referenceIDs.room) .pipe(switchMap((resarray:any)=>{ //... })); }` – mickdev Sep 30 '19 at 19:06
  • @mickdev , but getPointIdbyTags returns me array of length 1 and from that I want to get value and pass it on to 2nd api call , so how can I handle this, if I remove [0] then in the editor it shows an error , Property 'pipe' does not exist on type 'any[]' – Enthu Sep 30 '19 at 19:14
  • You don't have to use `pipe` with `switchMap`. `switchMap` is used to chain observables and you obviously don't get an observable from `getPointIdbyTags` but an array or string. So just pass that array or string to `this.siteService.getHisPointData`. – frido Oct 01 '19 at 10:10

1 Answers1

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

}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • @Enthu you are welcome. Please, show your code of method `getHisPointData`. :) – StepUp Oct 01 '19 at 10:35
  • it works, thanks for pointing it out, one query like I am displaying these values in html by interpolation (in Angular), something like this {{ a | b | c |}} but like if there is no value in a or b or c then I have to remove that particular | sign , what can be done for that – Enthu Oct 02 '19 at 06:09
  • @Enthu try to use ternary operator `?`. Something like this `{{ a ? a : b ? b : c ? c : "empty" }}`. [Read about ternary operator here.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) – StepUp Oct 02 '19 at 07:34
  • hi, suggestion on this please, https://stackoverflow.com/questions/58223425/to-assign-value-to-a-variable-on-subscribing-to-the-response-of-an-observable , thanks – Enthu Oct 04 '19 at 07:10