Herdy
Herdy

Reputation: 179

unable to get property 'status' undefined or null refrence Angular 4

I'm using angular 4 and ionic 3 & I get an error unable to get property 'status' undefined or null refrence from json that I display on html

this is my home.ts :

  @Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  data: Object;
  userDetails : Object;

 constructor(public navCtrl: NavController, public navParams: NavParams, public homeService: HomeServiceProvider, public app: App) {
        this.homeService.home().then((result) => {
        this.data = result ;
        //localStorage.setItem('token', this.data.users_username);
        this.userDetails = this.data['_body'];
        console.log(this.data['_body']) ;
        this.navCtrl.setRoot(MenuPage);
      }, (err) => {
        console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
    });
  }

dan ini home.html :

  <ion-header>
  <ion-navbar>
      <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
 <li>{{userDetails?.status}}</li>
</ion-content>

and this is my json srtucture :

{
        "status": 1,
        "message": "success"
}

is there anyone who can help me and tell me why ?? thanks

Upvotes: 0

Views: 506

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222692

It should work if your json is mentioned as above. if not try to parse it and then display it

this.userDetails = JSON.parse(this.data['_body']);

Upvotes: 1

Related Questions