I am trying to return data to my component and then trigger a route with data from service passed as a param. I think i'm using observables incorrectly and am confused if data can be returned to the subscribe or if I should be triggering routing from the service provider. The issue with triggering route from service provider is that the scope changes inside extractData. Can I access this.router from within extractData()? The component is set up like this on submit of a form:
Updated:
Component:
private dataFromServer: ShortFormModel;
onSubmit(shortForm: any) {
if (!shortForm) { return; }
const formModel = this.shortForm.value;
this.shortFormService.create(formModel)
.subscribe(body => { this.dataFromServer = body; console.log(body) })
})
}
service provider:
create(shortForm: any): Observable<ShortFormModel> {
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
return this.http.post(this.preCheckUrl, shortForm, options)
.map((res) => this.extractData(res))
.catch(
(error: any): Promise<any>=> {
this.router.navigate(['/error', error]);
return Promise.reject(error.message || error);
}
)
}
private extractData(res: Response) {
let body = res.json();
this.router.navigate(['/apply', body.appId])
return body|| { };
}
response payload:
{
"offer":{
"id":null,
"amount":null,
},
"dupes":true,
"appId":53
}