I have a rest service that returns macs from a scan, the json model and this:
{
"macs": [
{
"mac": "9C:5C:F9:66:73:34"
},
{
"mac": "B8:A3:E0:72:9E:EA"
},
{
"mac": "00:E0:4C:2A:26:60"
},
{
"mac": "00:E0:4C:76:0A:A7"
},
{
"mac": "00:E0:4C:0D:C7:58"
},
{
"mac": "00:E0:4C:79:7A:17"
},
{
"mac": "00:E0:4C:07:72:D9"
},
{
"mac": "00:E0:4C:60:97:77"
}
]
}
I have the following files:
mac.component
export class MacComponent implements OnInit {
@Input() mac:Mac
constructor() { }
ngOnInit() {
}
}
mac.model
export interface Mac{
mac:string
}
mac.service
@Injectable()
export class MacService {
constructor(private http: Http) { }
getMacs():Observable<Mac[]>{
return this.http.get('http://localhost:3000/macs')
.map(response => response.json())
}
}
macs.component
export class MacsComponent implements OnInit {
macs:Mac[]
constructor(private service:MacService) { }
ngOnInit() {
this.getMacs()
}
getMacs(){
this.service.getMacs().subscribe(macs => this.macs = macs)
console.log(this.macs);
}
printMacs(){
console.log(this.macs);
}
}
When I print in 'macs' it comes as undefined, I need an array of return macs