I am facing an issue with the Observable
parameter in my code. I have a consultService that contains the functions consult()
and response as a parameter. The function sendConsultRequest()
is called before using the response parameter. Although the sendConsultRequest function works perfectly fine and shows results on the page, I encounter an error when trying to export the content using the response parameter:
ERROR TypeError: Cannot read property 'dataList' of undefined
In my opinion, the response parameter should be initialized before the export action since the consultPage is working correctly. Could you please take a look at the service code below and help me identify why it's not functioning properly?
@Injectable()
export class ConsultService {
private url = 'api/consultApplication';
response: Application;
sendConsultRequest(id: string) {
console.log('sendConsultRequest');
this.http.get<Application[]>(this.url)
.subscribe(response => {
response.map(res => {
this.response = res;
this.updateApplication(res);
}),
pipe(catchError(this.handleError('sendConsultRequest', [])));
});
}
Below is the component for exporting data:
ngOnInit() {
console.log('ExportComponent init');
console.log(this.consultService.response);
this.application = this.consultService.response;
this.appID= this.consultService.response.dataList.find(pair => 'PROCEDURE' === pair.field)
? this.consultService.response.dataList.find(pair => 'PROCEDURE' === pair.field).value : null;
}
Any assistance will be greatly appreciated.