2017-10-03 3 views
2

私はjsonファイルからデータを抽出して配列に格納しようとしています。現在、私はこれらのエラーを取得しています:角度json.data()as array []

zone.js:2933 GET http://localhost:4200/app/persons.json 404 (Not Found) 

core.es5.js:1020 ERROR Error: Uncaught (in promise): undefined 
    at resolvePromise (zone.js:824) 
    at resolvePromise (zone.js:795) 
    at zone.js:873 

これは私のサービスである:

import { Injectable } from '@angular/core'; 
import { Actor } from '../model/actor'; 
// import { ACTORS } from './mock-actors'; 
import { Http, Headers } from '@angular/http'; 
import 'rxjs/add/operator/toPromise'; 

@Injectable() 
export class ActorService { 
    private actorsUrl = '/app/persons.json'; 
    constructor(private http: Http) { } 
    getActors(): Promise<Actor[]> { 
     return this.http.get(this.actorsUrl) 
      .toPromise().then(response => response.json().data as Actor[]) 
      .catch(this.handleError); 
    } 
    private handleError(error: any): Promise<any> { 
     console.error('Error: ', error); 
     return Promise.reject(error.message); 
    } 
} 

私のモデル

export class Actor { 
    lastName: string; 
    profession: string; 
    bio: string; 
    url: string; 
    imageUri: string; 
    name: string; 
    id: number; 
    realName: string; 
} 

JSONファイルは次のようになりますこの

{"personList":[{"lastName":"Wolowitz","profession":"Aerospace Engineer","bio":"Howard Joel Wolowitz, M.Eng is a fictional character on the CBS television series The Big Bang Theory, portrayed by actor Simon Helberg. Among the main male characters in the show, Howard is distinguished for lacking a doctoral degree, for still living with his mother, and for believing himself to be a \"ladies' man\". Simon Helberg's character is named after a computer programmer known by the show's co-creator Bill Prady.","url":"https://en.wikipedia.org/wiki/Simon_Helberg","imageUri":"Howard_Wolowitz.jpg","name":"Howard","id":"howard","realName":"Simon Helberg"},{"lastName":"Hofstadter","profession":"Experimental physicist","bio":"Leonard Leakey Hofstadter, Ph.D., is a fictional character on the CBS television series The Big Bang Theory, portrayed by actor Johnny Galecki. Leonard is an experimental physicist originally from New Jersey who shares an apartment with colleague and friend Dr. Sheldon Cooper (Jim Parsons). Leonard and Sheldon are named after actor/producer Sheldon Leonard, and Nobel Prize Laureates Robert Hofstadter and Leon Cooper.\nLeonard has been described as the straight man of the series. Penny (Kaley Cuoco) is Leonard's next-door neighbor and main love interest, and the teasing of romance between the two of them is a major force driving the series. For his portrayal, Galecki was nominated for a Primetime Emmy Award and a Golden Globe Award.","url":"http://en.wikipedia.org/wiki/Johnny_Galecki","imageUri":"Leonard_Hofstadter.jpg","name":"Leonard","id":"KLJSMLND","realName":"Johnny Galecki"}} 

EDIT

私はこの

{ 
"/app/*":{ 
"target": "appfoundry-restdemo.herokuapp.com/";, "secure": false, "logLevel": "debug" 
    } 
} 
+1

パスは現在のtsファイルからの相対パスである必要があります。あなたのパスを確認してください – Korte

+1

アクセスしているjsonファイルの相対パスを使ってみてください。 –

+0

jsonファイルに接続するためにプロキシを使用する必要があります。偽、 "ログレベル": { "/アプリ/ *":{ "ターゲット": "http://appfoundry-restdemo.herokuapp.com/"、 は "安全な" これは、プロキシファイルであります: "debug" } } – Gurbii

答えて

1

のようにプロキシを設定しているAPIへの接続を持っているために、このようにしてみてください。

応答メソッド内response.json()。data insted of response.json()。data as actor []

getActors(): Promise<Actor[]> { 
    return this.http.get(this.actorsUrl) 
     .toPromise().then(response => <Actor[]>response.json().data) 
     .catch(this.handleError); 
} 
+0

これは確かに404問題を修正しました!ネットワーク上で私は今オブジェクトを見ることができます。しかし、これを表示する方法を見つけようとしていますが、これは質問の一部ではありませんでした。 – Gurbii