I have the below method that calls an http observable
SaveWorkRequest(workRequest: any) {
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
let url = 'deployment/SaveWorkRequest';
let dto = { 'workResponse': workRequest };
// post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
return this.http.post(GlobalVariables.SITE_ROOT + url, dto, options)
//.toPromise()
//.then(this.extractData) //...and calling .json() on the response to return data
.map(this.extractData)
.catch(this.handleError);
}
//
private extractData(res: Response) {
let body = res.json();
console.log(body);
return body || {};
}
And this code in a component that calls it
let response = this.workRequestService.SaveWorkRequest(this.workRequest)
.subscribe(
hero => this.message = hero,
error => this.errorMessage = <any>error);
console.log(this.message);
The problem is, the code in the component returns before the service method. So console.log(this.message) is undefined. It must be a timing problem i guess?