In my code I have custom post method that extents the http class of angular 2.
post(url: string, body: string, options?: RequestOptionsArgs): Observable<any> {
if (!this._gs.externalRequest) {
let that = this;
this._gs.getToken().then((token) => {
if (token) {
options = this.prepareOptions(options, token);
}
return that.sendPostRequest(url, body, options);
});
}
else {
this._gs.externalRequest = false;
return this.sendPostRequest(url, body, options);
}
}
In above code post method returns an Observable whereas the method this._gs.getToken() in if condition reads token from the local storage its an asynchronous call and returns a promise.
Though the compilation doenst generate any error but, when I access
this.http.post('/api/myFormHandler', this.form.value) .subscribe((data) => { });
class MyFormComponent- inline template:16:29 caused by: Cannot read property 'subscribe' of undefined