2016-09-21 9 views
0

Web APIからムービー情報を取得するAngular2 beta 7でアプリケーションを作成しました。これはすべてうまくいきましたが、今はAngular 2の最終リリースで同じことをやろうとしています。自分のMovieInformationオブジェクトへのJSON情報のマッピングを除いて、ほとんどのアプリケーションを動作させることができます。JSONからObservableマッピングを取得することができません。

映画-information.ts:

export class MovieInformation { 
    constructor(
     public imdbID: number, 
     public title: string, 
     public year: number, 
     public genre: string, 
     public runtime: string, 
     public poster: string, 
     public director: string, 
     public writer: string, 
     public actors: string, 
     public plot: string, 
     public metascore: number, 
     public imdbRating: number, 
     public imdbVotes: number, 
     public type: string, 
     public favorite: boolean = false 
    ) 
    { 

    } 
} 

movie.service.ts

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

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

import {Movie} from '../model/movie'; 
import {MovieInformation} from "../model/movie-information"; 

const searchMoviesUrl = (title) => `http://www.omdbapi.com/?s=${title}`; 
const movieInformationUrl = (imdbId) => `http://www.omdbapi.com/?i=${imdbId}`; 

@Injectable() 
export class MovieService { 
    constructor(private http:Http) { 

    } 

    ... 

    getMovieInformation(movie:Movie): Observable<MovieInformation> { 
     console.log(movieInformationUrl(movie.imdbID)); 
     return this.http.get(movieInformationUrl(movie.imdbID)).map(result => result.json()) 
     .map(m => new MovieInformation(m.imdbID, m.Title, m.Year, m.Genre, m.Runtime, m.Poster, m.Director, 
       m.Writer, m.Actors, m.Plot, m.Metascore, m.imdbRating, m.imdbVotes, m.Type, movie.favorite)) 
      .catch(this.handleError); 
    } 

    private handleError(error: Response) { 
     console.error(error); 
     return Observable.throw(error.json().error || 'Server error'); 
    } 
} 

私がやろうとしている何がJSON結果(http://www.omdbapi.com/?i=tt1245526)をマップであります

{"Title":"RED", 
"Year":"2010", 
"Rated":"PG-13", 
"Released":"15 Oct 2010", 
"Runtime":"111 min", 
"Genre":"Action, Comedy, Crime", 
"Director":"Robert Schwentke", 
"Writer":"Jon Hoeber (screenplay), Erich Hoeber (screenplay), 
Warren Ellis (graphic novel), Cully Hamner (graphic novel)", 
"Actors":"Bruce Willis, Mary-Louise Parker, Heidi von Palleske, Karl Urban", 
"Plot":"When his peaceful life is threatened by a high-tech assassin, 
former black-ops agent Frank Moses reassembles his old team in a last 
ditch effort to survive and uncover his assailants.", 
"Language":"English, Russian", 
"Country":"USA", 
"Awards":"Nominated for 1 Golden Globe. Another 3 wins & 17 nominations.", 
"Poster":"http://ia.media-imdb.com/images/M/[email protected]@._V1_SX300.jpg", 
"Metascore":"60", 
"imdbRating":"7.1", 
"imdbVotes":"239,853", 
"imdbID":"tt1245526", 
"Type":"movie", 
"Response":"True"} 

の.map(m =>新しいMovieInformation(...))を使用している私自身のMovieInformationモデルクラスには、TypeScriptコンパイラがJSONからマッピング変数を認識することができません。 JSON。

誰かがこの問題の原因となっている間違っていることを教えてください。

+0

のように解決しようとすると、this.http.get(movieInformationUrl(movie.imdbID))が返されます。map(result => new MovieInformation(result.json()。imdbID、result.json()。Title ... それは動作しますが、私はいつも非常に醜いresult.json()を使用して見つける。 –

答えて

0

私が使用して固定された:溶液が(M:任意)であるのに対し

return this.http.get(movieInformationUrl(movie.imdbID)).map(result => result.json()) 
      .map((m: any) => new MovieInformation(m.imdbID, m.Title, m.Year, m.Genre, m.Runtime, m.Poster, m.Director, 
      m.Writer, m.Actors, m.Plot, m.Metascore, m.imdbRating, m.imdbVotes, m.Type, movie.favorite)) 
     .catch(this.handleError); 

をコンパイラは任意の変数を推定するように、任意のタイプのものとして、Mを定義するタイトルと年と同様に、任意から入手可能です私の例。だからm:anyはJSONレスポンスです。

mを何も入力せずに、コンパイラが変数を解決できず、アプリケーションを起動できないことがあります。この解決策により、これは私の問題を解決します。

関連する問題