Attempting to subscribe for a REST call
this.empObs0 = this.fetchData.getEmployeeInfoToUpdate(JSON.parse(this.empName0)).subscribe(
data0 => {
this.emp_DBRows0 = <EmpUpdateModel[]> data0;
},
error => {
console.log(error);
}
);
FetchDataService.ts
// Fetches employee information for updating
getEmployeeInfoToUpdate(jsonArray: JSON): Observable<EmpUpdateModel[]> {
console.log('GET getEmployeeInfoToUpdate');
this.dataGS = this.http.post<Observable<EmpUpdateModel[]>>('/api/getEmployeeInfoToUpdate', jsonArray);
return this.dataGS;
}
I need to unsubscribe in the ngOnDestroy()
method
// Destroying the component.
ngOnDestroy() {
console.log('Destroy called');
this.empObs0.unsubscribe();
}
However, encountering an error while trying to destroy it.
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'unsuscribe' of undefined
TypeError: Cannot read property 'unsuscribe' of undefined
at UpdateTeamRosterComponent.push../src/app/components/update-team-roster/update-team-roster.component.ts.UpdateTeamRosterComponent.ngOnDestroy (update-team-roster.component.ts:176)
at callProviderLifecycles (core.js:18943)
at callElementProvidersLifecycles (core.js:18911)
at callLifecycleHooksChildrenFirst (core.js:18901)
at destroyView (core.js:19963)
at callWithDebugContext (core.js:20722)
at Object.debugDestroyView [as destroyView] (core.js:20406)
at ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.destroy (core.js:18232)
at ComponentRef_.push../node_modules/@angular/core/fesm5/core.js.ComponentRef_.destroy (core.js:18068)
at RouterOutlet.push../node_modules/@angular/router/fesm5/router.js.RouterOutlet.deactivate (router.js:4858)
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at zone.js:873
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:14134)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:500)
at invokeTask (zone.js:1540)
Seeking assistance with resolving this issue.