如何将位置 ID 数组传递给服务 getLocationData?



如何将位置ID数组传递给服务?

我有位置 ID 数组

locationArr=[40871, 60009, 38149, 40868, 43240, 15299, 53897, 40976, 38151, 23183, 38152, 78579, 23180, 40977, 23176, 39565, 40884, 15298, 38147, 40966, 39669] 

实际上我需要将位置Arr传递给 http://192.168.7.45:9200/location/_doc/+locationArr

我需要将位置Arr上存在的位置ID数组传递给服务,以获取阵列位置Arr上每个位置ID的GPS1纬度和经度。

服务仅按位置 ID 获取位置 Id 仅适用于一个位置 ID,但对于位置数组,这是我的问题

getLocationData(id: number) {  
console.log("server "+id)  
return this.http.get('http://192.168.7.45:9200/location/_doc/'+id);  
}  

所以请如何在位置数组内循环实现它

calling service
this.partDetailsService.getLocationData(this.LocationId).subscribe(res => {          
this.dataLocation = res['_source']['GPS1'];     
var loc = this.dataLocation.split(',');      
this.lat = loc[0].trim();    
this.lng = loc[1].trim(); 

首先,您的后端需要更改为接受多个位置ID,似乎它只接受一个,然后基于此您可以发送数据。

如果您的后端在 GET 中支持多个 Id 作为逗号分隔值,那么您的角度代码将如下所示

getLocationData(ids: Array<number>) {  
console.log("server "+ids)  
return this.http.get('http://192.168.7.45:9200/location/_doc/'+ids.join(','));  
} 

如果您的后端支持多个 Id 作为 POST 主体数组,那么您的角度代码将如下所示

getLocationData(ids: Array<number>) {  
console.log("server "+ids)  
return this.http.post('http://192.168.7.45:9200/location/_doc/', { ids: ids });  
}  

如果您的后端不支持多个 ID,则需要循环访问 id,并为每个 id 调用后端

getLocationData(ids: Array<number>) {  
let observableBatch = [];
this.ids.forEach((id) => {
observableBatch.push(
this.http.get('http://192.168.7.45:9200/location/_doc/'+id)
.map((res) => res.json()));
});
return Observable.forkJoin(observableBatch);
}

this.partDetailsService.getLocationData(this.LocationIds).subscribe(res : Array<any> => {  
since it's an array you needs to loop through res        
res.forEach((item, index) => {
const dataLocation = res[index]['_source']['GPS1'];     
const loc = this.dataLocation.split(',');      
this.lat[index] = loc[0].trim();    
this.lng[index] = loc[1].trim();
});
}

总而言之,后端 api 签名驱动如何调用它

最新更新