My service functions in the following way:
getRecords(): Observable<any>{
return this.http.get(this.fetchAdminData)
.map(this.extractData)
.catch(this.handleError);
}
The extraction of data happens like this:
private extractData(res: Response) {
let body = res.json();
return body || { };
}
In my component, I make the call as shown below:
import {Component, OnInit} from '@angular/core'
import {FormsHandler} from '../services/service.forms'
@Component({
selector: 'form-admin',
templateUrl: '../partials/form5.html'
})
export class FormAdminComponent implements OnInit {
public records
constructor(private formHandler : FormsHandler){
}
ngOnInit(){
this.formHandler.getRecords().subscribe(res => {
if (res.ok){
this.records = res.data
console.log(res)
console.log(this.records[0])
}
})
}
}
However, when I include it in the HTML like this, errors occur:
{{records}} // This works perfectly
{{records[0]}} //Cannot read property '0' of undefined ERROR CONTEXT: [object Object]
Moreover, even accessing nested objects poses a challenge:
<tr *ngFor="let record of records">
<td>{{record.firstName + " "+ record.middleName+ " "+ record.LastName}}</td>
<td>{{record.BankingDetails.company}} </td> // This results in errorTypeError: Cannot read property 'company' of undefined
<td>{{record.BankingDetails}} </td> //working fine but resulting in [object Object]
<td>Pending</td>
</td>
</tr>
This leads to TypeError: Cannot read property 'company' of undefined
The response object looks like this:
Object {ok: true, data: Array[2]}
The complete data structure is as follows :
[
{
"Address": {
"addressLine1": "nh" ,
"addressLine2": "nghn" ,
"city": "ngh" ,
"formStatus": 2 ,
"pinCode": "ngh" ,
"state": "nghn"
} ,
"BankingDetails": {
"bankName": "csdcss" ,
"company": "cd" ,
"designation": "kn" ,
"loanAmount": "csd" ,
"loanPurpose": "cs" ,
"panCardNumber": "84894848" ,
"salary": "55"
} ,
"contact": "vsd" ,
"date": "vsd" ,
"dob": Mon Jan 01 1 00:00:00 GMT+00:00 ,
"email": <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb9a99939288939e90bb9c969a9297d5989496">[email protected]</a>, »
"firstName": "cs" ,
"firstname": "" ,
"formStatus": 3 ,
"gender": "male" ,
"id": "98fd72b9-62fe-4fcd-90d6-f2a5f83c052b" ,
"isAdmin": 1 ,
"lastName": "vs" ,
"lastname": "" ,
"middleName": "vds" ,
"middlename": "" ,
"month": "vsd" ,
"password": <binary, 60 bytes, "24 32 61 24 31 30..."> ,
"username": "" ,
"year": "vs"
},
...
]
I am struggling to understand why I can print JSON using console.log but cannot access it via HTML.
Furthermore, when I use
{{records}}
it shows up as [object Object]. Therefore, I have to add
{{records | json}}
to view the complete data.
Please advise on what I might be doing wrong, as I aim to access nested elements like records.BankingDetails.company.