Using the Heroes tutorial on the Angular 2 website. I have set up the heroService to use a rest service from tomcat. Using curl produces the correct result
C:\Temp\curl>curl http://centos7-ansible:8080/heroes/heroes
[{"id":1,"name":"Mr. Nice"},{"id":2,"name":"Narco"},{"id":3,"name":"Bombasto"},{"id":4,"name":"Celeritas"},{"id":5,"name":"Magneta"},{"id":6,"name":"RubberMan"},{"id":7,"name":"Dynama"},{"id":8,"name":"Dr IQ"},{"id":9,"name":"Magma"},{"id":10,"name":"Tornado"}]
So in the hero.service.ts,
private heroesUrl = 'http://centos7-ansible:8080/heroes/heroes'; // URL to web api
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}
In the calling component,
ngOnInit(): void {
console.log("about to get data");
this.heroService.getHeroes().then(heroes => this.heroes = heroes.slice(4, 8));
console.log("returned from getting data");
console.log(length of array is " + this.heroes.length );
}
Looking at the console I get
about to get data
GET http://centos7-ansible:8080/heroes/heroes 200 OK 43ms
returned from getting data
length of array is 0
So it works from curl but within Angular does not.
Also, looking at the access log in Tomcat, the same log message is for both curl and angular access.
"GET /heroes/heroes HTTP/1.1" 200 273
"GET /heroes/heroes HTTP/1.1" 200 273
Is the "273" the length of data returned? Which would suggest the data is there but is not being put in the Hero array?
Is my heroesUrl correct?
Within console, I get
EXCEPTION: Uncaught (in promise): TypeError: e is undefined
What does this mean?
What is "e"?
How do I go about debugging this as the console gives the code lines as part of a bundle.js.
Regards,
John
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => { console.log("service response text " + response.statusText);
console.log("service response data " + response.json());
console.log("service response status " + response.status);
return response.json();}
.catch(this.handleError);
}