Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What causes the Property does not exist on type error in this Angular 16 movies app?

I have been working on an SPA with Angular 16, TypeScript and The Movie Database (TMDB).

I run into a problem while working on displaying a movie’s details.

In app\services\movie-service.service.ts I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { HttpClient } from '@angular/common/http';
import { MovieResponse } from '../models/Movie';
import { Genre, GenreResponse } from '../models/Genre';

@Injectable({
  providedIn: 'root'
})

export class MovieService {

  constructor(private http: HttpClient) { }


  public getLatestMovies(): Observable<MovieResponse> {
    return this.http.get<MovieResponse>(`${environment.apiUrl}/movie/now_playing?api_key=${environment.apiKey}`);
  }

  public getMovieDetails(id: Number): Observable<MovieResponse>{
    return this.http.get<MovieResponse>(`${environment.apiUrl}/movie/${id}?api_key=${environment.apiKey}`);
  }
}

In app\models\Movie.ts I have:

export interface Movie {
    id?: number;
    adult?: boolean;
    backdrop_path?: string;
    poster_path?: string;
    title?: string;
    original_title?: string;
    tagline?: string;
    overview?: string;
    genre_ids?: any;
    release_date?: string;
    runtime?: number;
    vote_average?: string;
}

export interface MovieResponse {
    results?: Movie[];
    total_pages?: number;
    total_results?: number;
    page?: number;
}

In app\components\movie-details\movie-details.component.ts I have:

import { Component } from '@angular/core';
import { MovieResponse, Movie } from '../../models/Movie';
import { MovieService } from '../../services/movie-service.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-movie-details',
  templateUrl: './movie-details.component.html',
  styleUrls: ['./movie-details.component.scss']
})
export class MovieDetailsComponent {

  constructor(private movieService: MovieService, private activatedRoute: ActivatedRoute) { }

  public movieResponse!: MovieResponse;
  public movie_id: Number = 0;

  public showMovieDetails() {
    this.movie_id = Number(this.activatedRoute.snapshot.paramMap.get('id'));

    this.movieService.getMovieDetails(this.movie_id).subscribe((response) => {
      this.movieResponse = response;
    });
  }

  ngOnInit() {
    this.showMovieDetails();
  }
}

In app\components\movie-details\movie-details.component.html I try to display the movie’s title:

<div *ngIf="movieResponse">
  <h1>{{ movieResponse?.original_title }}</h1>
</div>

The problem

Insted of the desired result, I get this error in the browser:

TS2339: Property 'original_title' does not exist on type 'MovieResponse'.

Questions

  1. What am I doing wrong?
  2. What is the most reliable way to fix this issue?

>Solution :

This property ‘original_title’ does not exist on interface ‘MovieResponse’, exist on interface ‘Movie’
so you must change this line:

  public movieResponse!: MovieResponse;

to :

  public movieResponse!: Movie;

and update the service:

  public getMovieDetails(id: Number): Observable<Movie>{
    return this.http.get<Movie>(`${environment.apiUrl}/movie/${id}?api_key=${environment.apiKey}`);
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading