Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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": [email protected], »
"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

like image 510
Abhishek Soni Avatar asked Jul 17 '16 16:07

Abhishek Soni


1 Answers

You are fetching records async and when Angular tries to resolve bindings the first time records is still null therefore records[0] fails.

You can instead use

{{records && records[0]}}

For other expressions you can use the safe-navigation (Elvis) operator like

{{records?.someprop}}

but there is no safe-navigation for array index access (?[])

like image 114
Günter Zöchbauer Avatar answered Oct 20 '22 11:10

Günter Zöchbauer