Can't read the property "name" of undefined


Haiii. I am the new one to ionic as well as angular. For practice, I am developing an ionic3 app. My app contains a service page “countries.service.ts” which reads countries details in the form of JSON from the URL using HTTP. And my home page get the data from the “CountriesService”. But i am getting the error “Can’t read property ‘name’ of undefined”…
In home.html I am trying to read the values like this.:
public countryObj:any;
this.countriesService.getCountryDetail().subscribe(res => this.countryObj= res.json()[0]);
public getCountryDetail(){
let url:string=‘’;
return this.http.get(url);
Any help will be appriciated.
Thanks in advance…


The following line:
this.countriesService.getCountryDetail().subscribe(res => this.countryObj= res.json()[0]);

Is likely to be executed after your template is rendered, and so countryObj will be undefined. You should at least initialize it to be any empty object, or to some sensible defaults.
E.g. public countryObj = {};


Thank you @SigmundFroyd for your suggestion… It is working Now. Thank u a lot.
I used like this and problem solved…

public countryObj={name:'',capital:''};