2017-02-16 2 views
0

omdbapi.com APIを使用してムービーデータ用の簡単な検索エンジンを作っています。私は、ビューを作成するためのデータとコンポーネントを取得するためにサービスを設定しているが、私は、私はエラーを取得するHTMLに接続しようとすると:Angular2の外部ソースからJSONを取得した後の結果を表示する方法

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Movieクラス

export class Movie { 
    constructor(
     Title: string, 
     Year: string, 
     Rated: string, 
     Released: string, 
     Runtime: string, 
     Genre: string, 
     Director: string, 
     Writer: string, 
     Actors: string, 
     Plot: string, 
     Language: string, 
     Country: string, 
     Awards: string, 
     Poster: string, 
     Metascore: string, 
     imdbRating: string, 
     imdbVotes: string, 
     imdbID: string, 
     Type: string, 
     Response: string 
    ) {} 

} 

をここにあります私のコンポーネント:

import { Component, OnInit } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import { Subject } from 'rxjs/Subject'; 

//observable class extensions 
import 'rxjs/add/observable/of'; 
import 'rxjs/add/operator/switchMap'; 
import 'rxjs/add/operator/map'; 
//observable operators 
import 'rxjs/add/operator/catch'; 
import 'rxjs/add/operator/debounceTime'; 
import 'rxjs/add/operator/distinctUntilChanged'; 

import { MovieSearchService } from './movie-search.service'; 
import { Movie } from './movie'; 

@Component({ 
    selector: 'movie-search', 
    templateUrl: './movie-search.component.html', 
    styleUrls: ['./movie-search.component.css'], 
    providers: [MovieSearchService] 
}) 

export class MovieSearchComponent implements OnInit { 
    movies: Observable<Movie[]>; 
    private searchTerms = new Subject<string>(); 

    constructor(
     private movieSearchService: MovieSearchService, 
     private http: Http 
    ) {} 

    search(term:string) { 
     return this.searchTerms.next(term); 
    } 

    ngOnInit(): void { 
     this.movies = this.searchTerms 
     .debounceTime(300) // wait 300ms after each keystroke before considering the term 
     .distinctUntilChanged() // ignore if next search term is same as previous 
     .switchMap(term => term // switch to new observable each time the term changes 
     // return the http search observable 
     ? this.movieSearchService.search(term) 
      // or the observable of empty heroes if there was no search term 
     : Observable.of<Movie[]>([]) 
     ) 
     .catch(error => { 
      console.log("--------- Error -------"); 
      console.log(error); 

      return Observable.of<Movie[]>([]); 
     })  
    } 
} 

これはサービス

import { Injectable } from '@angular/core'; 
import { Http, Jsonp } from '@angular/http'; 

import { Observable} from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 

import { Movie } from './movie'; 

@Injectable() 
export class MovieSearchService { 
    constructor(
     private http: Http, 
     private jsonp: Jsonp 
    ) {} 

    search(term: string): Observable<Movie[]> { 
     return this.http 
      .get(`http://www.omdbapi.com/?s=${term}`) 
      .map(response => { 
       return response.json().each() as Movie[] 
      }) 
    } 
} 
です

とビュー

<div class="col-xs-12"> 
    <input #searchBox id="search-box" /> 
    <button (click)="search(searchBox.value)">Search</button> 
</div> 

<ul *ngIf="movies"> 
    <li *ngFor="let movie of movies"> 
     {{ movie.title }} 
    </li> 
</ul> 

どのように私は、ビューは、各映画の映画のタイトルを表示させることが? お時間をいただきありがとうございます。

+0

[httpとObservableの角2のカンニング使用データ](http://stackoverflow.com/questions/41961693/http-with-observable-in-angular-2-cant-use-data)の可能な複製 – AngularChef

+0

APIから返されたJSON構造のサンプルを投稿してください –

答えて

1

常にネットワークタブをチェックして、実際にデータを受信して​​いるかどうか、そのデータがどのように表示されているかを確認してください。

これをテストします。どうやら最初にすべてのこのようなあなたのURLを構築する必要があります:

return this.http.get('http://www.omdbapi.com/?s='+term) 

次に応答へと、それは次のように構築されています:

{"Search":[{"Title":"24","Year":"2001–2010","imdbID".... 

あなたは配列を取得するために抽出する必要がありますだから何:

.map(response => {response.json().Search}) 

およびコンポーネントにサブスクライブ:

this.movieSearchService.search(term) 
    .subscribe(d => this.movies = d) 
その後、

、あなたのタイトルを表示する:

<ul *ngIf="movies"> 
    <li *ngFor="let movie of movies"> 
     {{ movie.Title }} 
    </li> 
</ul> 

は、上記のコードでSearchTitleに注意してください。これは大文字と小文字が区別されるため、{{ movie.Title }}と大文字のTを使用してデータを表示する必要があります。

これで問題は解決します。 :)

+0

返信ありがとうございます@ AJT_82 –

+0

ようこそ! :)あなたのデベロッパーツールを使用してください(例:F12のchrome)。次に、ネットワークタブを開き、リクエストを確認し、応答が得られたかどうかを確認します。あなたのURLには空のレスポンスがあり、そのためにあなたは反復できませんでした。ここに写真があります。私が黄色を付けた部分を確認してください:) http://imgur.com/a/RuClo – Alex

+0

申し訳ありませんが、これは私の終了を間違って投稿しました。私はエラーを取得しています:

Argument of type '(term: string) => Subscription | Observable' is not assignable to parameter of type '(value: string, index: number) => ObservableInput'. Type 'Subscription | Observable' is not assignable to type 'ObservableInput'. 
私は映画などの観察可能なオブジェクトとして映画を宣言するためであると考えている:観察可能 ムービーの構造(タイトル、年、等が含まれています...) Observableとして宣言していない場合は、どのように変数ムービーを宣言する必要がありますか? –

関連する問題