legendarny ziom
legendarny ziom

Reputation: 75

Angular 2+ Cannot read property 'id' of undefined

I have a strange error and I cant find out why. According to :id from route, I'm getting table privateLessons, next searching that one object with correct id and showing into my template. In the end, I'm getting effect that I want, data shows correctly but im getting two same errors in console:

ERROR TypeError: Cannot read property 'id' of undefined
at Object.eval [as updateRenderer] (DetailedAnnouncementComponent.html:3)
at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13094)
at checkAndUpdateView (core.es5.js:12241)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
at execEmbeddedViewsAction (core.es5.js:12559)
at checkAndUpdateView (core.es5.js:12237)
at callViewAction (core.es5.js:12601)

How fix it?

detailed-announcement.html

  <div class="detailed-announcement">
    {{ privateLesson.id }}
    {{ privateLesson.subject }}
    {{ privateLesson.category }}
    {{ privateLesson.price }}
    {{ privateLesson.level }}
    {{ privateLesson.voivodeship }}
    {{ privateLesson.city }}
    {{ privateLesson.place }}
    {{ privateLesson.description }}
    {{ privateLesson.author }}
  </div>

detailed-announcement.tmp

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { PrivateLessonsService } from './../../../../services/private-lessons.service';

import { PrivateLesson } from './../../../../classes/private-lesson';


@Component({
  selector: 'app-detailed-announcement',
  templateUrl: './detailed-announcement.component.html',
  styleUrls: ['./detailed-announcement.component.scss']
})
export class DetailedAnnouncementComponent implements OnInit {

  privateLessons: PrivateLesson[];
  privateLesson: PrivateLesson;
  privateLessonIdFromParams: number;

  constructor(
    private _privateLessonsService: PrivateLessonsService,
    private _activatedRoute: ActivatedRoute  
  ) { }

  ngOnInit() {

    this._activatedRoute.params.subscribe(params => {
      this.privateLessonIdFromParams = params['id'];

    });

    this._privateLessonsService
      .getPrivateLessons()
      .subscribe(
        responseData => {
          this.privateLessons = responseData;
          this.privateLesson = this.getDimensionsByFind(this.privateLessons, this.privateLessonIdFromParams);

        }
      );

  }

  getDimensionsByFind(array: any[], id: number){
    return array.find(x => x.id === id);
  }

}

Upvotes: 2

Views: 5055

Answers (1)

UncleDave
UncleDave

Reputation: 7188

You're trying to show the data before the server has responded, since you're making an async request for the data.

Put an *ngIf on your div to prevent it from trying to show the data too early.

  <div class="detailed-announcement" *ngIf="privateLesson">
    {{ privateLesson.id }}
    {{ privateLesson.subject }}
    {{ privateLesson.category }}
    {{ privateLesson.price }}
    {{ privateLesson.level }}
    {{ privateLesson.voivodeship }}
    {{ privateLesson.city }}
    {{ privateLesson.place }}
    {{ privateLesson.description }}
    {{ privateLesson.author }}
  </div>

Upvotes: 7

Related Questions