My code:
In service.ts:
postRequest(path, body, header): Observable<Object> {
var needToken = this.utilityService.needAccessToken();
if (needToken) {
var tokenRequest = {};
tokenRequest[Static.TOKEN_REQUEST_USERNAME] = localStorage.getItem("this.encrypted_emailid");
tokenRequest[Static.TOKEN_REQUEST_PASSWORD] = localStorage.getItem("this.encrypted_password");
var tokenReqs = tokenRequest
var tokenHeaderJSON = {};
tokenHeaderJSON[Static.HEADER_CONTENT_TYPE] = Static.HEADER_APPLICATION_JSON;
var header: any = new Headers(tokenHeaderJSON);
this.tokenRequest(Static.TOKEN_URL, body, header).subscribe(
response => {
var message = response[0].json;
var status = response[0].status;
if (status == 200) {
this.utilityService.tokenrequest(path, body, header);
}
},
error => {
var message = error[0].json;
var status = error[0].status;
}, // error
() => {
}) // complete
}
else {
var status: number;
var message: any;
var reason;
var url = this.appendURL(path);
var headString = JSON.stringify(header);
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'application/json', 'Access-Control-Allow-Methods': 'POST' });
let option = new RequestOptions({ headers: header });
var bodyString = JSON.stringify(body);
return this.http.post(url, body, option).map(
(res: Response) => {
var res_string = JSON.stringify(res);
var json_res = JSON.parse(res_string);
var fin_json_res = json_res._body;
status = res.status;
return [{ status: res.status, json: fin_json_res }];
})
.catch((err: any) => {
var err_string = JSON.stringify(err);
var json_err = JSON.parse(err_string);
var fin_json_err = json_err._body;
var jsonErr = JSON.parse(fin_json_err);
status = err.status;
message = jsonErr.message;
return Observable.throw([{ status: err.status, json: fin_json_err }]);
});
}
}
tokenRequest(path, body, header): Observable<Object> {
var status: number;
var message: any;
var reason;
var url = this.appendURL(path);
var headString = JSON.stringify(header);
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'application/json', 'Access-Control-Allow-Methods': 'POST' });
let option = new RequestOptions({ headers: header });
var bodyString = JSON.stringify(body);
return this.http.post(url, body, option).map(
(res: Response) => {
var res_string = JSON.stringify(res);
var json_res = JSON.parse(res_string);
var fin_json_res = json_res._body;
status = res.status;
return [{ status: res.status, json: fin_json_res }];
})
.catch((err: any) => {
var err_string = JSON.stringify(err);
var json_err = JSON.parse(err_string);
var fin_json_err = json_err._body;
var jsonErr = JSON.parse(fin_json_err);
status = err.status;
message = jsonErr.message;
return Observable.throw([{ status: err.status, json: fin_json_err }]);
});
}
And in component.ts:
getAllThemes() {
var tokenReqs = this.utilityService.apitimestampClub();
var tokenHeaderJSON = {};
tokenHeaderJSON[Static.HEADER_CONTENT_TYPE] = Static.HEADER_APPLICATION_JSON;
var header = new Headers(tokenHeaderJSON);
var Body = {};
Body[Static.REQUEST_APITS] = tokenReqs;
var body = JSON.stringify(Body);
this.appComponent.spinFlag = true;
this.postToServer(header, body);
}
postToServer(header, body) {
this.utilityService.postRequest(Static.THEME_URL, body, header).subscribe(
response => {
var message = response[0].json;
var status = response[0].status;
var msg = JSON.stringify(response);
var parsed_Msg = JSON.parse(message);
var arr_Parsed_Msg = parsed_Msg.Themes;
if (status == 200) {
this.utilityService.consoleFunction(message);
}
},
error => {
var message = error[0].json;
var status = error[0].status;
if (status == 440 || status == 401) {
this.router.navigate(['/home']);
this.appComponent.spinFlag = false;
}
else {
//else block
}
}, () => {
//complete
})
}
Here, before post request am checking whether token request is needed? If it is true, then need to call token request post request in this.tokenRequest(...) line, and from the response it need to continue the normal post request that i called in success of token request post request response. And in my component getAllThemes() need to wait until token request success followed by normal post request response, but in my case, it calls for token request but did not wait for the response of this request, before the response it return to getAllThemes() request, so am getting error as undefined.
Generally how to make another post request within post request and main post request subscribe need to wait until complete the sub post request in angular 2?
Can anyone help for this?