I'm working on angular and try to populate the data from the child object's key value. The JSON data is here :
"other_lessons": [
{
"id": 290,
"name": "Christmas Test #290",
"course": {
"id": 43,
"name": "Christmas Test ",
"description": "",
"teacher": {
"id": 4,
"name": "Sandy's Teacher",
"email": "[email protected]",
"role": "TEACHER",
"token": "abcd",
"about": "Blah blah blah ",
"phone": "2222222222",
"image_url": "xyz",
"payment_information": {}
}
]
What I'm trying to do is to get the details of the data of course.name and course.id. And I' getting this error all the time in my developer console.
ERROR TypeError: Cannot read property 'name' of undefined
at Object.eval [as updateRenderer] (ng:///AppModule/AppTileComponent.ngfactory.js:30)
at Object.debugUpdateRenderer [as updateRenderer] (vendor.bundle.js:105951)
at checkAndUpdateView (vendor.bundle.js:105102)
at callViewAction (vendor.bundle.js:105445)
at execComponentViewsAction (vendor.bundle.js:105377)
at checkAndUpdateView (vendor.bundle.js:105103)
at callViewAction (vendor.bundle.js:105445)
at execComponentViewsAction (vendor.bundle.js:105377)
at checkAndUpdateView (vendor.bundle.js:105103)
at callViewAction (vendor.bundle.js:105445)
What but working fine for the id and name in the JSON data. The code I implemented is like this, I'm passing the details to the selector tag i.e.,
<widget-app-tile>
homepage.component.html
<widget-app-block title="You may be interested in">
<div class="row">
<div class="col-md-6" *ngFor="let lesson of lessons">
<widget-app-tile [lesson]="lesson"></widget-app-tile>
</div>
</div>
</widget-app-block>
app-tile.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Lesson } from '../../models/models';
@Component({
selector: 'widget-app-tile',
templateUrl: './app-tile.component.html',
styleUrls: ['./app-tile.component.css']
})
export class AppTileComponent implements OnInit {
@Input() lesson : Lesson = <Lesson> {}
constructor() { }
ngOnInit() {
}
}
app-tile.component.html
<div class="content-details">
<div class="details">
<h5>{{lesson.course.name}}</h5>
<img class="icon" src="/assets/images/logo-transparent.png" alt="No Internet" align="right"/>
</div>
As you can see that if I call the {{lesson.course.name}} it throws up an error but when I call this {{lesson.name}} or {{lesson.id}} it works fine and shows up the data.
I have used the Cerialize Library and my model class is this :
model.ts
import { deserialize, deserializeAs, Deserialize } from 'cerialize';
/* UTILTY METHODS */
export function cast(data, modelClass) {
return Deserialize(data, modelClass)
}
export function castCollection (collection, modelClass) {
return collection.map( item => cast(item, modelClass))
}
/* MODELS */
export class Course {
@deserialize id: number
@deserialize name: string
}
export class Lesson {
@deserialize id: number
@deserialize name: string
@deserializeAs(Course) course : Course
}
So this is the model which I'm using to get the data, not using interfaces.
EDITS
I have tried getting the results on my home page itself and the results are coming fine, but in using but I don't know why the app-tile.component.ts is not working fine. The ocde which I implemented is as follows :
homepage.component.html
<div class="" *ngFor="let lesson of lessons" >
#{{lesson.id}} - {{lesson.name}}; Course: {{lesson.course.name}}
</div>
<widget-app-block title="You may be interested in">
<div class="row">
<div class="col-md-6" *ngFor="let lesson of lessons">
<widget-app-tile [lesson]="lesson"></widget-app-tile>
</div>
</div>
</widget-app-block>
And here it is working fine. And one more details are as follows :
homepage.component.ts
export class MyClassesComponent implements OnInit {
lessons : Lesson[] = []
constructor(private http : HttpClient) { }
ngOnInit() {
}
ngAfterViewInit(){
this.http.get("/ui/student/home").subscribe( data => {
this.lessons = castCollection(data['other_lessons'], Lesson)
})
}