我正在进行一次api调用,并期望它可以将响应传递给第二个api调用,但出现错误
错误TypeError:this.helperService.getPointIdbyTags(...)[0] .pipe不是函数
在线.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),
)
}));
}
1st api调用是通过getPointIdbyTags函数
进行的getPointIdbyTags(buildingObj: any, zoneTags: any, roomRef: string = undefined) {
let pt = [];
if (typeof buildingObj.buildings === 'undefined') {
if (typeof
buildingObj.entities !== 'undefined') {
buildingObj.entities.filter(entity => {
let matchEntity = zoneTags.every(elem => entity.markers.indexOf(elem) > -1);
if (matchEntity) {
pt.push(entity._id);
}
})[0];
} else {
buildingObj.filter(entity => {
let matchEntity = zoneTags.every(elem => entity.markers.indexOf(elem) > -1);
if (matchEntity) {
pt.push(entity._id);
}
})[0];
}
} else {
let equipObj = buildingObj.floors.map(floor => floor.entities.map(room => room.entities.filter(equip => equip.referenceIDs.room == roomRef)));
equipObj[0].map(equip => equip.filter(device => {
if (device.entities.length > 0) {
return device.entities.filter(entity => {
let matchEntity = zoneTags.every(elem => entity.markers.indexOf(elem) > -1);
if (matchEntity) {
pt.push(entity._id)
}
})[0];
} else {
let matchEntity = zoneTags.every(elem => device.markers.indexOf(elem) > -1);
if (matchEntity) {
pt.push(device._id)
}
}
}));
}
if (pt) {
return pt;
}
}
1st api调用getPointIdbyTags响应
0:“ 5d8df1fb3942df083a4539c5”
长度:1
然后我试图将其传递给
switchMap((resarray:any)=>{
const res = resarray[0]
return this.siteService.getHisPointData(res, 'current')
[[0]看起来是一个简单的函数,它返回一个数组。因此,不需要使用getPointIdbyTags(buildingObj: any, zoneTags: any, roomRef: string = undefined) { ... }
和.pipe()
运算符,因为使用RXJS可以更轻松地组成异步或基于回调的代码。
您的代码应如下所示:
.switchMap