2

I've got a tree of three components where i pass down the json data from backend using property binding altough it gets displayed i always get errors in the console that

Cannot read property 'id' of null at ListCategoryComponent.ngOnInit

I tried the answer to similar threads to use different lifecycle hooks in List-category.component.ts (ngOnChanges and AfterContentInit) but still if doesnt work.

List.component.html - where i retrieve JSON from backend and pass it down into its child Forum-list-section.component

<app-list-section *ngFor="let section of sections" [section]="section" >
      </app-list-section>

List-section.component.ts - then extract categories from section and pass it further down

@Input() section;
  categories;

  ngOnInit() {
    this.categories = this.section['categories'];
  }

List-section.component.html

<app-list-category
    *ngFor="let category of categories" [category]="category"
    class="forum__post">
  </app-list-category>

List-category.component.ts where I finally i retrieve and display the recieved data

@Input() category: Category;
  categoryId: string;
  latestThreadId: string

 ngOnInit() {
    this.categoryId = `${this.category.id }-${this.category.slug}`;
    this.latestThreadId = `${this.category.latest_thread.id}-${this.category.latest_thread.slug}`;
  }
  • 1
    It means that the category passed from the parent is null. You may not access properties of an object if there is no object. – JB Nizet May 12 '18 at 11:27
  • Check this question https://stackoverflow.com/questions/41169281/angular-2-passing-data-to-child-component-after-parent-initialisation?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa I think its similar to what you are asking – Nandita Sharma May 12 '18 at 11:30

0 Answers0