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;
}
因此,该注释非常正确,必须在不使用此关键字的情况下调用不在服务中且在函数内部声明的变量。
places = res.results.map ....
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;
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.