【问题标题】:How to pass array of locationId to service getLocationData?如何将 locationId 数组传递给服务 getLocationData?
【发布时间】:2020-04-18 19:46:40
【问题描述】:

如何将 locationId 数组传递给服务?

我有一组位置 ID

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

其实我需要将 locationArr 传递给http://192.168.7.45:9200/location/_doc/+locationArr

我需要将 locationArr 上存在的 locationId 数组传递给服务,以获取数组 locationArr 上每个 locationId 的 GPS1 纬度和经度。

服务通过 locationId 仅针对一个 locationId 获取位置,但对于位置数组,这是我的问题

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(); 

【问题讨论】:

    标签: angular typescript angular-material angular7 angular-resource


    【解决方案1】:

    首先您的后端需要更改为接受多个locationId,似乎它只接受一个,然后您可以在此基础上发送您的数据。

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

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

    如果您的后端将支持多个 Id 作为 POST 正文数组,那么您的 Angular 代码将如下所示

    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 签名驱动如何调用它

    【讨论】:

    • 我需要通过前端来实现它,所以我可以通过使用请求数组和使用 fork join 来做到这一点,你能帮我吗
    • 如果你的后端不支持multipleId,需要循环调用
    • 你能帮我如何在位置数组中循环
    • 以及如何在组件内部调用它来获取经纬度 this.partDetailsS​​ervice.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();
    • @ahmedbarbary 你会收到一个数组,所以你需要循环遍历它
    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2023-03-14
    • 2022-08-19
    相关资源
    最近更新 更多