I'm trying to subscribe to a service method that loads movie data from an API. Whenever the search is performed it searches for a different movie with a new ID. How do I subscribe to this from a component?
What I have done so far:
Service:
search(id): Observable<Movie> {
return this.http.get<Movie>('https://api.themoviedb.org/3/movie/' + id + '?api_key=KeyHidden&language=en-US');
}
Component:
export class TitleComponent implements OnInit {
movie: Movie;
constructor(public ms: MovieService) {}
ngOnInit() {
this.ms.search(this.ms.movieid).subscribe(data => this.movie = data);
}
}
Button performing search:
<button type="submit" mdbBtn (click)="ms.search(ms.movieid)" id="sub"> Search
</button>
Interface Movie:
export interface Movie {
title: string;
}
- Serving the app throws an error because the id by default is undefined. How do I subscribe without getting an error.
- Why the search button isn't working?
- How do I get only the values that are defined in the interface and discard everything else?