I'm following the Angular tour of heroes examples and have constructed (I think) my version of the code identically, but am not receiving the behavior I expect.
My service
import { Injectable } from '@angular/core';
import { PORTS } from './mock-ports'
import { Port } from './port'
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UpdateportsService {
private controller_url = '/gelante/getports/150013889525632'
private controller_ip = 'http://localhost:8080'
getPorts(): Observable<Port[]> {
return this.http.get<Port[]>(this.controller_ip + this.controller_url)
}
constructor(private http: HttpClient) { }
}
myObserver (used for debugging)
const myObserver = {
next: x => console.log('Observer got a next value: ' + x),
error: err => console.error('Observer got an error: ' + err),
complete: () => console.log('Observer got a complete notification'),
};
getPorts (subscribes to the observable service)
// This is part of the same class as getPorts
ports: Port[] = [];
getPorts(): void {
// To subscribe to an observable, you take the declared observable, which in
// this case is getPorts (which returns an array of ports) and then you
// subscribe to it. Anytime the observable is called it will emit values
// which are then sent to all subscribers.
console.log(this.ports)
this.updateportsService.getPorts().subscribe(ports => this.ports = ports);
// This prints all of my port data as expected
this.updateportsService.getPorts().subscribe(myObserver);
console.log(this.ports)
}
Full output from Debug Console
Array(0) []
switch.component.ts:76
Array(0) []
switch.component.ts:82
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
core.js:40917
Observer got a next value: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
switch.component.ts:13
Observer got a complete notification
switch.component.ts:15
[WDS] Live Reloading enabled.
Goal
The goal is to take a listing of switch interfaces I'm receiving from a REST API (separate from Angular) and assign them to a list of dictionaries called ports. This should be accomplished in the line:
this.updateportsService.getPorts().subscribe(ports => this.ports = ports);
Problem
In the tour of heroes example ports in the function getPorts should be populated. I have confirmed from both Wireshark and some debug output that the HTTP get request is functioning as expected. Specifically, you can see the line:
this.updateportsService.getPorts().subscribe(myObserver);
That it receives a big array of objects (as expected). However, for whatever reason the assignment in ports => this.ports = ports
does not seem to work. The value of ports is always an empty array with zero elements. However, I haven't been able to figure out why.