Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 http get json issue

Trying to display http json response in angular2 view. To get me started I have completed a specific tutorial for .NET core and Angular2 with Typescript. Everything worked fine, so I tried to modify it a bit but it looks I got stuck and can not find the answer. This is what I have so far.

wall.main.component.ts

import {Component, OnInit} from "angular2/core";
import {CORE_DIRECTIVES} from "angular2/src/common/directives/core_directives";
import {WallService} from "./wall.service";

@Component({
    selector: "wall",
    templateUrl: "app/components/wall/wall.main.html",
    providers: [WallService],
    directives: CORE_DIRECTIVES
})
export class WallComponent implements OnInit {
    data: any;

    constructor(private service: WallService) { }

    ngOnInit() {
        this.get();
        console.log(this.data);
    }

    get() {
        this.service.get().subscribe((json: any) => {
            console.log(json);
            this.data = json;
        });
    }
}

wall.service.ts

import "rxjs/Rx"
import {Http} from "angular2/http";
import {Injectable} from "angular2/core";

@Injectable()
export class WallService {
    constructor(private http: Http) { }

    get() {
        return this.http.get("wall/getwallposts").map(response => response.json());
    }
}

wall.main.html

<wall>
    <p>{{data.Id}}</p>
    <p>{{data.Body}}</p>
</wall>

Html does not display data. The error:

TypeError: Cannot read property 'Id' of undefined in [{{data.Id}} in WallComponent@3:7]

Tried a lot of ways. Accessing as an array with the index data[0].Id, changing variable types, nothing. Also this console.log(json) works fine. I can see and read the object in the console, but the other one console.log(this.data) shows undefined, which I would expect to be the same. What am I missing?

like image 976
DasBoot Avatar asked Apr 07 '16 19:04

DasBoot


People also ask

How to use parameters with HTTP GET () in Angular 2?

To use parameters with Http.get(), find the link. Angular 2 Http get() Parameters + Headers + URLSearchParams + RequestOptions Example. Note: Angular version 4.3 has introduced HttpClient to perform HTTP requests. If we are using Angular version 4.3 or higher, we should use HttpClient.

What is HTTP client in Angular 2?

Angular HTTP library provides Http client for server communication. get() is the Http client method that uses HTTP GET method to communicate server using HTTP URL.

Which class performs HTTP requests in angular?

The Angular HttpClient class performs HTTP requests. The HttpClient is available as an injectable class. It has methods to perform HTTP requests.

What is the use of GET () method in angular?

get () is the method of angular Http API that interacts with server using HTTP GET method. It accepts a HTTP URL and returns Observable instance. RequestOptionsArgs is optional.


1 Answers

On view when CD started, data.Id interpolation tries to evaluate, but data object isn't defined yet. So when it tried to access Id property from it, it throw an error.

<wall>
    <p>{{data?.Id}}</p>
    <p>{{data?.Body}}</p>
</wall>

Other thing I wanted to point out is, there is no meaning to have wall element again, as you are already rendering a wall component.(I know it is not gonna work, because we haven't provided it inside directives option of Component)

like image 174
Pankaj Parkar Avatar answered Oct 13 '22 08:10

Pankaj Parkar