Currently, I am in the process of incorporating a base service into an Angular application. This service is designed to provide methods to other services for composing requests with default headers and options. However, I encounter an issue when attempting to call the get or post methods. These methods simply invoke this.request
, but I am encountering a ZoneAwarePromise error.
ERROR Error: Uncaught (in promise): Cannot read property 'request' of undefined
at resolvePromise (zone.js:831)
at zone.js:896
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17280)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at drainMicroTaskQueue (zone.js:601)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
at invokeTask (zone.js:1744)
at HTMLButtonElement.globalZoneAwareCallback (zone.js:1770)
defaultErrorLogger @ core.js:15714
...
This error is perplexing because I have confirmed that this.request
exists by logging its value before using it within the post method.
interface Options {
payload?: any,
headers?: any,
options?: any
}
@Injectable()
export class ApiService {
private host: string = `${environment.apiUrl}`;
constructor(private http: HttpClient) {}
public get(route: string, options?: any) {
return this.request(route, 'GET', options);
}
public post(route: string, options?: any) {
return this.request(route, 'POST', options);
}
public setToken(token: string) {
localStorage.setItem('Token', token);
}
public unsetToken() {
localStorage.removeItem('Token');
}
private request(route: string, method: string, requestOptions?: any): Promise<void> {
return new Promise((resolve, reject) => {
...
});
}
}
I am seeking assistance to resolve this error and successfully call the request method without encountering any issues.
EDIT
The post method is being called within the following login
method:
@Injectable()
export class AuthService {
private user: any = null;
constructor(private service: ApiService) {
...
}
...
}