Reputation: 2163
I have an Angular Service and I want to return a promise with a Typed Array but i'm allways getting this error: src/app/city.service.ts(52,22): error TS2339: Property 'places' does not exist on type 'CityService'. I have no idea of what I am doing wrong.
getPlace(coordinates : Coordinates) {
// var places : Array<Place> = [];
let promise = new Promise((resolve, reject) => {
this.http.get('http://localhost:3000/api/place/', {params: coordinates})
.toPromise()
.then(
res => { // Success
var places: Array<Place>;
this.places = res.results.map(item => {
var place = new Place();
place.place_id = item.place_id;
place.name = item.name;
place.vicinity = item.vicinity;
place.coordinates = new Coordinates();
place.coordinates.latitude = item.geometry.location.lat;
place.coordinates.longitude = item.geometry.location.lng;
return place;
});
resolve(this.places);
},
msg => { // Error
reject(msg);
}
);
});
return promise;
}
Upvotes: 1
Views: 235
Reputation: 5026
This give the same result, without the need to manually create a new Promise which resolve when the inner promise resolve. It results in less code boilerplate and better readability.
getPlace(coordinates : Coordinates) {
return this.http.get('http://localhost:3000/api/place/', {params: coordinates})
.toPromise()
.then(res => {
var places: Place[];
places = res.results.map(item => {
var place = new Place();
place.place_id = item.place_id;
place.name = item.name;
place.vicinity = item.vicinity;
place.coordinates = new Coordinates();
place.coordinates.latitude = item.geometry.location.lat;
place.coordinates.longitude = item.geometry.location.lng;
return place;
});
return places;
});
}
Upvotes: 1
Reputation: 1955
So the comment is pretty correct, variables that are not the part of a service, and were declared inside functions must be called without this keyword.
places = res.results.map ....
Upvotes: 3