javascript - Angular TypeError: Cannot read property 'color' of undefined trying to read data -


i trying read data locally stored file in angular 4.

here same data:

  {     "color": "blue"   } 

and here code component.ts file:

import { component, oninit } '@angular/core'; import { http } '@angular/http';  @component({   selector: 'app-home',   templateurl: './home.component.html',   styleurls: ['./home.component.css'] }) export class dashboardcomponent implements oninit {    data;    constructor(private http: http) {     this.http.get('../../assets/data.json')       .subscribe(res => this.data = res.json());      console.log(this.data.color);   }    ngoninit() {   }  } 

i keep getting error:

error: uncaught (in promise): typeerror: cannot read property 'color' of undefined

what i'm doing wrong here / how can fix it?

place console.log inside subscribe

this.http.get('../../assets/data.json')   .subscribe(res => {      this.data = res.json();      console.log(this.data.color);   } ) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -