Good morning, I am new at Angular2 and I have some problems with the use of Observables and components
ERROR to be fixed
ERROR Error: Can not find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays
I have a REST service that returns a JSON with certain information. From the component I created, I call this service:
Component.ts
ngOnInit() {
this.getSystems();
}
getSystems(): void {
this.systemService.getSystems()
.subscribe(systems => this.systems = systems);
}
The problem is that I do not deal with an information array, but with an object, so I can not use the command * ngfor
Component.html
<li *ngFor="let system of systems">
<a routerLink="/systemDetail/{{system.systemId}}">
<span class="badge">{{system.systemId}}</span>
{{system.host}}
{{system.password}}
{{system.port}}
{{system.rfcDest}}
{{system.userId}}
{{system.version}}
</a>
</li>
As I've read, the solution would be to create an @Pipe but I'm not sure how it works. Can someone explain it to me?
JSON
{
"test":
[
{
"host": "h265",
"messagingSystemID": "1",
"password": "xxx,
"port": "50000",
"rfcDest": "testDes",
"systemID": "PPO",
"userId": "TestUser",
"version": "7.5"
},
{
"host": "h455",
"messagingSystemID": "2",
"password": "xxx",
"port": "50000",
"rfcDest": "testDes2",
"systemID": "PPO",
"userId": "TestUser",
"version": "7.5"
}
]
}
Expected response My intention is to print these systems in text, table, or any other way possible. But all of them.
Service - Option 1 This is the first solution I tried to make
/** GET systems from the server */
getSystems (): Observable<System[]> {
var data = this.http.get<System[]>(this.systemsUrl);
console.log('getSystems service');
console.log(data);
return data;
}
Service - Solution 2 I have also tried to call the service in the following way:
getSystems(): Observable<System[]> {
let url = 'http://10.8.0.2:50000/integration.experts~web~mappingtesttool/rest/systems';
console.log(url);
return this.http.get(url)
.map(this.extractData);
}
private extractData(res: Response) {
let body = res.json();
console.log("extraData");
return body || { }; // here
}
Could anyone tell me what I'm failing? I am indifferent to use option 1 or option 2 of the service. Greetings