Reputation: 1076
I create network services that should send get request with 1 param, and I should get the result from the API,
NetworkServices.ts
import { Injectable } from '@angular/core';
import {Http} from "@angular/http";
@Injectable()
export class NetworkServices{
constructor(public http:Http){
}
getCurrency(obj){
console.log("function fired!")
let url = 'http://api.fixer.io/latest?base='+obj.selectedCurrency;
console.log(obj);
this.http.get(url)
.toPromise()
.then( (res) => {
return res;
}
)
}
}
the results should be at res, this was work 3 weeks ago, I dont know how angular manage to change heh..
the error code is:
[19:46:04] transpile update started ...
[19:46:06] typescript: D:/ionic/firstApp/src/services/network.ts, line: 25
Property 'toPromise' does not exist on type 'Observable<Response>'.
L24: return this.http.get(url).toPromise();
[19:46:06] transpile update failed L25:
}
[19:46:06] watch ready
hope you guys can solve this :)
Upvotes: 0
Views: 145
Reputation: 691635
Your method doesn't return anything, and the then callback doesn't do anything other than transforming a Promise<Response>
into another Promise<Response>
. So your whole method is basically a noop.
The code should probably look like
getCurrency(obj){
console.log("function fired!")
let url = `http://api.fixer.io/latest?base=${obj.selectedCurrency}`;
console.log(obj);
return this.http.get(url).toPromise();
}
Or, if you want to return a promise containing the json body of the response rather than the response itself
getCurrency(obj){
console.log("function fired!")
let url = `http://api.fixer.io/latest?base=${obj.selectedCurrency}`;
console.log(obj);
return this.http.get(url).toPromise().then(res => res.json());
}
Regarding your compilation error, you need to import the toPromise operator:
import 'rxjs/add/operator/toPromise';
Upvotes: 1
Reputation: 657118
You need to add
import 'rxjs/add/operator/toPromise'
Upvotes: 1