问题
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