Im trying to use the data from an api call but the data is not returned when I'm trying to access it so im getting undefined. Heres the code:
API Service:
public addBusiness(businessObject)
{
// Testing endpoint dor fake endpoint
let endPoint = '/business';
// let endPoint = this.baseUrl+`/business`;
let content = {businessObject: businessObject};
let headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(content);
return this.http.post(endPoint, body, options )
.map(res => res.json());
}
Component:
this.BusinessApiService.addBusiness(businessObject).subscribe(res => {
// Check for an success response if there is do:
if (res.request.status === 'success') {
console.log('%c addBusiness API call was a success', 'color: green; font-weight: bold;');
console.log(res);
let currentQuoteObj = {
businessId: res.data.businessId,
applicantIds: []
};
this.quoteHandler.saveCurrentQuote(currentQuoteObj);
let businessId = this.quoteHandler.currentQuote['businessId'];
let that = this;
this.applicants.value.forEach(function(applicant) {
// console.log(applicant);
console.log(businessId);
that.BusinessApiService.addApplicant(applicant, businessId).subscribe(res => {
if (res.request.status === 'success') {
let appicantId = res.data.applicantId;
that.quoteHandler.addApplicanToCurrentQuote(appicantId);
}
else {
// Todo add error handling
console.log('ITS BROKE');
}
});
});
console.log('Current Quote object:');
console.log(this.quoteHandler.currentQuote);
console.log('Current Uncompleted Applicant object:');
console.log(this.quoteHandler.currentUncompletedApplicants);
}
// If there is an error handle it here
else {
// Todo add error handling
console.log('%c addBusiness API call was a fail', 'color: red; font-weight: bold;');
// console.log(res);
}
});
The add applicant api is basically the same as the add business api call. How would i get this to work so it does what i want it to do but it will not just get undefined on the businessId and appicantId. Thanks!
EDIT: json im expecting back from API:
{
"request" : {
"status" : "success",
"action" : "Business Created"
},
"data":{
"businessId" : 1021
}
}