2017-10-19 146 views
0

在我的离子项目中,我使用API​​获取航班详细信息。在该API中,它具有名为“airportResources” 的对象并且具有4个值(出发终端,到达终端,出发网关,到达网关)。但有时这个对象在api中不可用,或者所有的值都不可用(这意味着它有时仅包含出发端,或到达终端或任何两个或三个值)。 我的问题是,当我尝试显示此值时,它显示错误。 我可以如何处理这个问题。下面包括在响应的 屏幕快照(此图像的airportResources对象中只有2个值) enter image description here如何处理API数据

TS代码:

enter code here 

get_arrivalflights(){

var communicationParams = { 
    showLoader: true, 
    reqUrl: "", 
    reqBody: {}, 
    callBackSuccess: (response) => { 
    console.log(response); 
    this.objectData = response.flightStatuses; 

    }, 
    callBackFailure: (response) => { console.log("failure dude"); console.log(response); } 
}; 
this.restservice.makeGetRequest(communicationParams); 

}

HTML:

<div class="col" style="font-size: 18px;text-align: center">{{item.airportResources.departureTerminal}}</div> 

阿比电话中提供:

makeGetRequest(communicationParams: any) { 

    this.baseUrl = " the url i want to call "; 


    var loader; 
    if (communicationParams.showLoader == true) { 
     loader = this.loadingController.create({ 
     content: "" 
     }); 
     loader.present(); 
    } 
    this.http.get(this.baseUrl + communicationParams.reqUrl) 
     .map(response => response.json()) 
     .subscribe(communicationParams.callBackSuccess, function(respone) { 
      if (communicationParams.showLoader == true) { 
      loader.dismiss() 
      } 
      communicationParams.callBackFailure(respone); 
     }, 
     () => { 
      console.log('Authentication Complete'); 
      if (communicationParams.showLoader == true) { 
      loader.dismiss() 
      } 
     }); 
    } 

错误消息:(?) enter image description here

回答

0

在HTML中,使用aysnc运营商等来显示和可选的参数得不到数据不存在时结构上的错误。

{{ (item | async)?.airportResources.departureTerminal }}