Observable 的链式调用不适用于管道、地图
Chain calls of Observable not working with pipe, map
我这里有几个函数,它们在成功响应时不断向接收到的响应添加一些有用的数据,并将其作为参数发送到另一个 HTTP 调用,最后 return 将所有内容都发送到父函数。第一次调用后,我无法获取数据。
getBulkDataForSelectedids(inputIDs) { //an array of objects with key as "etype" and corresponding ids
if (inputIDs !== null && inputIDs.length > 0) {
let observableArray: any = [];
for (var i = 0; i < inputIDs.length; i++) {
const ids = inputIDs[i]["IDs"];
const entityType = inputIDs[i]["Type"];
ids.forEach((id) => {
observableArray.push(this.getbulkInfoBulkArrayIdURL(entityType, id));
});
}
observableForkJoin(observableArray).subscribe(
(response: any) => {
console.log(response); //wait for all response
},
(error: any) => {
console.log(error);
}
);
}
}
getbulkInfoBulkArrayIdURL(etype, guid) {
return this.service
.getCall( // simple HTTP get call in the service
"/api/entities/" +
etype +
"/" +
id
)
.pipe(
map((response: object) => {
const bulkInfoBulkArrayIdURL = response["bulkInfoBulkArrayIdURL"];
return this.getsampleCount(bulkInfoBulkArrayIdURL, response);
}),
catchError((err) => {
return of(null);
})
);
}
getsampleCount(url, bulkInfoResponse) {
return this.service.getCall(url).pipe( // simple HTTP get call in the service
flatMap((response) => {
let sampleCount = response["recordCount"];
return this.getBulkArrayData(url, sampleCount, bulkInfoResponse);
}),
catchError((err) => {
return of(null);
})
);
}
getBulkArrayData(url, sampleCount, bulkInfoResponse) {
return this.service
.getCall(url + sampleCount) // simple HTTP get call in the service
.pipe(
map((response: object) => {
return this.ConvertBulkArray(response, bulkInfoResponse);
}),
catchError((err) => {
return of(null);
})
);
}
ConvertBulkArray(response: object, bulkInfoResponse) {
// uses this two parameters and create output
const output = {
property1 : object,
};
return output;
}
在 getbulkInfoBulkArrayIdURL
方法中,我成功地从行 const bulkInfoBulkArrayIdURL = response["bulkInfoBulkArrayIdURL"];
获得了输出,但是随后对 return this.getsampleCount(bulkInfoBulkArrayIdURL, response);
的调用没有 return 任何东西。这是实现链接的最佳方式还是我遗漏了什么?感谢您的帮助。
您的代码看起来有点乱,但我认为这里的问题是在您的 getBulkArrayData
方法中,您将 map
通过管道传递给可观察对象。这会创建一个二阶可观察对象,在这种情况下您可能不想要它。如果将 map
更改为 switchMap
,它也会在收到值时订阅。
我这里有几个函数,它们在成功响应时不断向接收到的响应添加一些有用的数据,并将其作为参数发送到另一个 HTTP 调用,最后 return 将所有内容都发送到父函数。第一次调用后,我无法获取数据。
getBulkDataForSelectedids(inputIDs) { //an array of objects with key as "etype" and corresponding ids
if (inputIDs !== null && inputIDs.length > 0) {
let observableArray: any = [];
for (var i = 0; i < inputIDs.length; i++) {
const ids = inputIDs[i]["IDs"];
const entityType = inputIDs[i]["Type"];
ids.forEach((id) => {
observableArray.push(this.getbulkInfoBulkArrayIdURL(entityType, id));
});
}
observableForkJoin(observableArray).subscribe(
(response: any) => {
console.log(response); //wait for all response
},
(error: any) => {
console.log(error);
}
);
}
}
getbulkInfoBulkArrayIdURL(etype, guid) {
return this.service
.getCall( // simple HTTP get call in the service
"/api/entities/" +
etype +
"/" +
id
)
.pipe(
map((response: object) => {
const bulkInfoBulkArrayIdURL = response["bulkInfoBulkArrayIdURL"];
return this.getsampleCount(bulkInfoBulkArrayIdURL, response);
}),
catchError((err) => {
return of(null);
})
);
}
getsampleCount(url, bulkInfoResponse) {
return this.service.getCall(url).pipe( // simple HTTP get call in the service
flatMap((response) => {
let sampleCount = response["recordCount"];
return this.getBulkArrayData(url, sampleCount, bulkInfoResponse);
}),
catchError((err) => {
return of(null);
})
);
}
getBulkArrayData(url, sampleCount, bulkInfoResponse) {
return this.service
.getCall(url + sampleCount) // simple HTTP get call in the service
.pipe(
map((response: object) => {
return this.ConvertBulkArray(response, bulkInfoResponse);
}),
catchError((err) => {
return of(null);
})
);
}
ConvertBulkArray(response: object, bulkInfoResponse) {
// uses this two parameters and create output
const output = {
property1 : object,
};
return output;
}
在 getbulkInfoBulkArrayIdURL
方法中,我成功地从行 const bulkInfoBulkArrayIdURL = response["bulkInfoBulkArrayIdURL"];
获得了输出,但是随后对 return this.getsampleCount(bulkInfoBulkArrayIdURL, response);
的调用没有 return 任何东西。这是实现链接的最佳方式还是我遗漏了什么?感谢您的帮助。
您的代码看起来有点乱,但我认为这里的问题是在您的 getBulkArrayData
方法中,您将 map
通过管道传递给可观察对象。这会创建一个二阶可观察对象,在这种情况下您可能不想要它。如果将 map
更改为 switchMap
,它也会在收到值时订阅。