0

Hey guys I am using the OMDB API to get the list of movies. I get the data in the console but I cant loop through it to show it in the UI. It just returns an empty.

export class example {
  movies: Movie[];
  search: string;

  constructor(private service: MovieService) {}

  getMovies(): void {
    this.service.get(this.search).then((result) => {
      this.movies = [result];
      console.log(result)
    });
  }
}

When I console log the data I am getting this: enter image description here in my HTML component I loop through it like this:

<div *ngFor="let movie of movies">
<div>{{movie.Title}}<span>{{movie.Rated}}</span></div>
</div>

But this is not working. It just doesnt do anything.

Can someone help me please? I would really appreciate it.

Thanks

9
  • 1
    Please, try to change the this.movies = [result]; to this.movies = result.Search; and lets see what will happening Commented Mar 27, 2017 at 0:08
  • it says Search does not exists on Type movie. Commented Mar 27, 2017 at 0:11
  • as I can see on your "print", the result object has a property called Search that is an array with the data you are looking for. You need to pass this value to your movies list. Commented Mar 27, 2017 at 0:15
  • How would i pass it? Commented Mar 27, 2017 at 0:18
  • Your service.get(...) is not returning a Movie object. It's returning an object like: {Search: Movie[]; totalResults: number; Response: boolean}. You need to change this signature. Just to make a quickly check try to use this: this.movies = (<any>result).Search; Commented Mar 27, 2017 at 0:21

1 Answer 1

2

Take a look on your MovieService class. The get(...) function needs to return a promise with an "typed" object like:

{
    Search: Movie[]; 
    totalResults: string;
    Response: string;
}

and not a Movie object. The current typing is wrong. To check it, just try to change the line:

this.movies = [result];

to:

this.movies = (<any>result).Search;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.