Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

angular - read JSON using angular2

I have problem with jsonobject I'M trying to pull the data out from it and I failed.

I have this API that I pull my data from: http://api.fixer.io/latest?base=CAD

I have placed it on variable results, If i want to get to object paramater date,base,rate like below:

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

I get the error code:

[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 

Its feel weird that I can't pull the data out, what could it be?

get currency function

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());
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try updating your getCurrency() to just return the promise by removing the then():

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

Then the solution from @pe8ter should work:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...