Im getting 'TypeError: Cannot read properties of undefined (reading 'owner')' because...

Discussão em 'Angular' iniciado por Jango, Novembro 10, 2024.

  1. Jango

    Jango Guest

    Although the app works fine despite the error, I am receiving that error in my console because my component has not loaded the 'post' observable from the backend through the postService. My code is:

    Component

    export class PostComponent {
    post!: Post;

    constructor(
    activatedRoute: ActivatedRoute,
    private postService: PostService,
    private timeFormatService: TimeFormatService
    ) {
    activatedRoute.params.subscribe((params) => {
    if (params['id'])
    postService.getPostById(params['id']).subscribe((serverPost) => {
    this.post = serverPost;
    });
    });
    }
    }


    My html just displays post values such as {{post.owner}} or {{post.title}} in paragraphs or headings.

    Im getting that error for each post value. Ive figured i can fix this error by setting default values by changing post!:post; to:

    post:post = {owner: '', title: ''}


    And when the postService gets the real post details from the backend it updates the values. Im sort of new to coding, is this the correct way to do it?

    Continue reading...

Compartilhe esta Página