0
votes

I am developing an angular4 application. My Component.ts looks like:

ngOnInit() {

this.http.get('http://localhost:4000/data/').subscribe(data => this.temp = data);
// this.temp1 = JSON.parse(this.temp)
console.log(this.temp)

}

On the console I am getting 'undefined' for this.temp

1
add import statement for Response and typecast inside the map - Aravind
So you're actually using Angular 4, not Angular 2. Since you seem to be just starting, forget about Http. It's deprecated in Angular 5. Use HttpClient. angular.io/guide/http - JB Nizet
Thanks you, I am using HttpClient. Now I am not getting that error. But I am getting - Property 'json' does not exist on type 'Object'. - naik3
With the new http service you don't need to call json() any more. It's decoded automatically, see angular.io/guide/http#typechecking-the-response - martin
OK. Read my previous comment. Print the value inside of the callback function passed to subscribe(). HTTP is asynchronous. So the callback function is execute later, when the response comes back. - JB Nizet

1 Answers

1
votes

Write your console.log like this

this.http.get('http://localhost:4000/data/').subscribe(data => {this.temp = data; console.log(this.temp)});