I'm trying to read a local json file and parse it into a class that i made which have the same properties. When i try to read from the class, it gives me errors saying the class is null or undefined.
I have a file hall.ts which looks like this:
import {Item} from '../item/item';
export class Hall {
constructor(public id:number,
public naam:string,
public oppervlakte:number,
public aantalItems:number,
public itemsMetNodigeActie:number,
public items:Item[]) {
}
}
It uses item.ts:
export class Item {
constructor(public categorie:string,
public naam:string,
public productnummer:string,
public omschrijving:string,
public laatsteUitgevoerdeActie:Actie,
public eerstVolgendeActie:Actie) {
}
}
export class Actie{
constructor(datum: string,
type: string,
omschrijving: string){}
}
The json file, hall1.json, that i'm trying to read from looks like this:
{
"id": 1,
"naam": "hall1",
"oppervlakte": 100,
"aantalItems": 3,
"itemsMetNodigeActie": 3,
"items": [
{
"id": 1,
"categorie": "machine",
"productnummer": "ADE124e",
"omschrijving": "print papieren af",
"laatsteUitgevoerdeActie": {
"datum": "2015-01-05T00:00:00.000Z",
"type": "vervanging",
"omschrijving": "papier vervangen"
},
"eerstVolgendeActie": {
"datum": "2016-01-06T00:00:00.000Z",
"type": "vervanging",
"omschrijving": "inkt vervangen"
}
}
]
}
I'm using a hall.service.ts which tries to read the json file which is locally stored, and returns it in an Hall object. This is the methode for doing that:
public getHall(): Observable<Hall> {
return this.http.get('app/hall/hall1.json')
.map((res:Response) => res.json());
}
I use this method in my hallDetail.component.ts
export class HallDetailComponent implements OnInit{
public hall: Hall;
constructor(
private service: HallService
){}
ngOnInit(){
this.service.getHall().subscribe((hall:Hall) => {
this.hall = hall;
})
}
}
So far it doesn't give me any errors, but when i try to read from the hall object, it sais that it is undefined
@Component({
template: `
<div>
{{hall.naam}}
</div>
`
})
Error :
EXCEPTION: TypeError: Cannot read property 'naam' of undefined in [
{{hall.naam}}
in HallDetailComponent@1:7]