In my Angular 4 application, I have a method that have to return an Observable. This method has 3 conditions. First and second conditions make a get call, but the third condition does nothing and in this case I have to return an Observable as well, because this method is the first part of a .flatmap operator. So to chain the second part of the .flatmap operator, I need an Observable from the first part.
I've tried return new Observable<void>();, but I've got an error:
ERROR TypeError: Cannot read property 'subscribe' of undefined
This is the initial method that calls a service to load the data.
loadModelData() {
this.customerService.getModelDetail(this.code)
.subscribe(response => {
this.selected = response.body as Customer;
});
}
}
This is the service method that have to chain 2 calls.
getModelDetail(code) {
const params = new HttpParams().set('projection', 'withBalance');
return this.endPointUrlService.loadMaps(this.entityLink)
.flatMap((res) => {
return this.http.get(this.endPointUrlService.cutLinks(
this.endPointUrlService.map.get('customers')) + '/' + code, {observe: 'response', params: params})
.map((response) => <any>response);
})
}
And this is the methods from a support service. checkIfMapIsReady() is a method that returns a void Observable in the third case:
loadMaps(entityLink: EntityLink[]): Observable<void> {
console.log(entityLink);
for (const i in entityLink) {
if (entityLink.hasOwnProperty(i)) {
return this.checkIfMapIsReady(entityLink[i].name, entityLink[i].searchMap, entityLink[i].endPoints[0])
}
}
}
public checkIfMapIsReady(modelName: string, mapName: string, endPoints: string) {
if (this.map.get(modelName) === undefined) {
console.log('endpoint url service All maps undefined ' + modelName);
return this.getLinks(modelName, this.mapNames.get(mapName), false)
} else {
console.log('endpoint url service Populate only ' + mapName);
if (this.mapNames.get(mapName).get(endPoints) === undefined) {
return this.getLinks(modelName, this.mapNames.get(mapName), true)
} else {
return new Observable<void>();
}
}
}