首先想解释一下我想要实现的目标。我从数据库中获取餐厅,然后添加与用户位置和餐厅位置的计算距离。我将其作为属性添加到餐厅对象的位置。然后我想根据从附近到远处的距离对我的结果进行排序。
但承诺结果(有距离的餐厅)不包含距离。
这是我尝试过的代码,控制台日志返回数组 WITH distance 但是当我在 chrome 调试器中设置断点时,我看不到该属性。
这是承诺calculateDistance function:
calculateDistance(restaurants: Array<Restaurant>): Promise<Array<Restaurant>> {
const promise = new Promise<any>((resolve, reject) => {
// const restaurantDistances = [];
restaurants.map((restaurant) => {
const restaurantLocation: LatLng = new LatLng({
lat: restaurant['Restaurant']['Lat'],
lng: restaurant['Restaurant']['Long']
});
this.locationService.getUserLocation().then(() => {
this.googlemapService.initGoogleMapsApi().then(() => {
const distance = this.googlemapService.computeDistanceBetween(this.locationService.location, restaurantLocation);
restaurant['Restaurant']['Distance'] = distance;
// restaurantDistances.push(restaurant);
console.log(restaurants, 'restMap', restaurant, distance);
resolve(restaurants);
});
}).catch( error => {
console.log('error = ', error);
});
});
});
return promise;
}
这是在成功函数内部:
this.calculateDistance(restaurants).then((restaurantsDist) => {
console.log('after Calc distance', restaurantsDist, restaurants);
this.determinInstanceStorage(fetchMethodName, restaurantsDist, resolve);
});
有人可以帮帮我吗,我用一种map方法解决了结果,也许这是导致问题的原因?