Abhishek Soni Abhishek Soni - 11 days ago 7
TypeScript Question

Angular 2 - Cannot read property '0' of undefined error with context ERROR CONTEXT: [object Object]

my service is like this

getRecords(): Observable<any>{
return this.http.get(this.fetchAdminData)
.map(this.extractData)
.catch(this.handleError);
}


and Extract data is as following

private extractData(res: Response) {
let body = res.json();
return body || { };
}


In my component i am calling this as follows

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])
}
})
}

}


But when this is called on as follows in html it errors

{{records}} // this is working perfectly
{{records[0]}} //Cannot read property '0' of undefined ERROR CONTEXT: [object Object]


also i even cannot access my nested object like this

<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>


results in TypeError: Cannot read property 'company' of undefined

my response is like this

Object {ok: true, data: Array[2]}


and complete data is like this :

[
{
"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": abhishek@gmail.com, »
"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 did't getting what am i doing wrong becuase its working fine with console.log and printing a json but is not accessible with html

also i noted that when i am using

{{records}}


its resulting in [object Object]

i have to explicitly write

{{records | json}}


to get my complete data

Hey please suggest me what i am doing wrong i want to access my nested elements like records.BankingDetails.company

Answer

I finally got a solution to this problem as per @Akshay Rao suggestions i used these so that data is available before rendering of page

<div *ngIf="records">
</div>

also as per @Günter Zöchbauer suggestion i do require to use (Elvis) operator like

{{record.BankingDetails?.company}}

and it worked well . Thanks