2016-10-27 7 views
-1

json \ objectに問題があります。データを取り出そうとしていて、失敗しました。angular2を使用してJSONを読み取る

私はから私のデータを引き出し、このAPIを持っている:私は iは以下のようなのparamater日、ベース、率をobjectに取得したい場合は、変数resultsの上に置かれている http://api.fixer.io/latest?base=CAD

calc(details) { 
    let results = [this.networkServices.getCurrency(details)]; // object is here "getCurrency deal with the GET request. 
    alert(results.base); 
} 

私は、エラーコードを取得する:

[02:58:36] transpile update started ... 
[02:58:38] typescript: D:/ionic/firstApp/src/pages/currency/currency.ts, line: 19 
      Property 'base' does not exist on type 'Promise<any>[]'. 

     L18: let results = [this.networkServices.getCurrency(details)]; 
     L19: alert(results.base); 

[02:58:38] transpile update failed 

その私が引くことができないという奇妙な感じデータが出て、何ができますか?

は、サービス要求が非常に要求の結果がオブジェクトではなく、オブジェクト自体に解決約束非同期である

getCurrency(obj){ 
    console.log("function fired!") 
    let url = `http://api.fixer.io/latest?base=${obj.selectedCurrency}`; 
    return this.http.get(url).toPromise().then(res => res.json()); 
    } 
+0

をネットワークサービスのgetCurrency()メソッドで更新してください –

+0

これはAngularと何が関係していますか?これはJSONと何が関係していますか? –

答えて

1

通貨機能を得ます。このような何かを試してみてください:

this.networkServices.getCurrency(details).then(result => alert(result)) 
+0

angular2は受け入れていません。その後、赤でマークしてください。 – itzikb

+0

何がエラーですか? – pe8ter

2

だけthen()を除去することによって、約束を返すために、あなたのgetCurrency()を更新してみてください。

getCurrency(obj){ 
    console.log("function fired!") 
    let url = `http://api.fixer.io/latest?base=${obj.selectedCurrency}`; 
    return this.http.get(url).toPromise(); 
    } 

その後pe8ter @からソリューションが動作するはずです:

this.networkServices.getCurrency(details).then(result => alert(result))

関連する問題