2017-08-26 31 views
-1

app.component.ts文件,我在[Object Object]alert(this.datas);alert(this.datas.ReportDtDataForMAPP);形式取回數據給出空白結果作爲一個結果是,當我嘗試訪問該報告的網址從UI,我得到這個錯誤:Cannot read property 'URL' of undefined無法讀取的未定義的屬性「URL」 - 訪問JSON對象

有人可以提供一個解決方案嗎?

下面是必要的文件:

app.component.ts類(節選)

//... 
//Lines of code 
//... 

this.myMobileDataService.checkAuthorization(this.enterpriseId,v2Token) 
    .subscribe(
     response => { 
      this.datas = response; 

      JSON.stringify(this.datas); 

      alert(this.datas); 

      alert(this.datas.ReportDtDataForMAPP); 

      if(this.datas == null || this.datas == ""){ 
       this.nav.setRoot(this.accessDeniedPage, {showFooter : false}); 
      } else { 
       this.nav.setRoot(this.homePage, {showFooter : false}); 
      } 
     }, 
     err => { 
      console.log("Error here!:"+err);   
     } 
     ); 

    }, 


//... 
//Lines of code 
//... 

OpenReport(report : any) 
{ 
     let list = this.datas.ReportDtDataForMAPP; 
     let newList = list.filter((t) => t.SubReport == report); 
     JSON.stringify(newList) 
     this.myURL = newList[0].URL; 
     this.openUrl(this.myURL); 

} 

//... 
//Lines of code 
//... 

app.html(節選)

//... 
//Lines of code 
//... 
<ion-grid> 

    <ion-row> 
     <ion-col class="reportHeader reportHeaderPadding"><strong>AAA</strong></ion-col> 
    </ion-row> 
    <ion-row text-center> 
     <ion-col (click)="OpenReport('MURLAAA')" tappable><img src = "assets/icon/AAA.png" alt = "Image Not Available" class = "homeImages"></ion-col> 
     <ion-col (click)="OpenReport('MURLBBB')" tappable> <img src = "assets/icon/BBB.png" alt = "Image Not Available" class = "homeImages"></ion-col> 
     <ion-col (click)="OpenReport('MURLCCC')" tappable> <img src = "assets/icon/CCC.png" alt = "Image Not Available" class = "homeImages"></ion-col> 
    </ion-row> 
    <ion-row text-center class="addBorder"> 
     <ion-col (click)="OpenReport('MURLAAA')" tappable class="reportName">AAA</ion-col> 
     <ion-col (click)="OpenReport('MURLBBB')" tappable class="reportName">BBB</ion-col> 
     <ion-col (click)="OpenReport('MURLCCC')" tappable class="reportName">CCC</ion-col> 
    </ion-row> 

    //... 
    //Lines of code 
    //... 

myMobileDataService。 ts class

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map'; 
import { Headers,RequestOptions } from '@angular/http'; 
import { Observable } from 'rxjs'; 
import { Platform } from 'ionic-angular'; 


@Injectable() 
export class myMobileDataService { 

    constructor(private http: Http, private platform: Platform) {} 

    public checkAuthorization(_enterpriseID: string, _token : any) : Observable<any>{ 

    let details = { 
        "EntId" : "_enterpriseID" 
        }; 

    let body = JSON.stringify(details); 

    let headers = new Headers(
     { 
      'Content-Type': 'application/json', 
      'Authorization' : 'bearer ' + _token 
     } 
    ); 

    let options = new RequestOptions({headers:headers}); 

    return this.http.post('myURL',body,options) 
      .map((res) => res.json()) 


    } 

} 

HTTP POST請求以這種形式提供了數據:

ReportDtDataForMAPP:[ 
{ 
    "MainReport": "AAA", 
    "SubReport": "aaa", 
    "URL": "https://[email protected]" 
}, 
{ 
    "MainReport": "BBB", 
    "SubReport": "bbb", 
    "URL": "https://[email protected]" 
}, 
{ 
    "MainReport": "CCC", 
    "SubReport": "ccc", 
    "URL": "https://[email protected]" 
} 


] 
+0

相反警報(this.datas)的',','console.log'的'this.datas'序列化JSON來檢查服務真的返回你正在尋找的東西對於。 –

+0

@SayanPal這也是打印[對象對象] – Debo

+1

這看起來不正確,因爲'console.log(JSON.stringify(this.data))'應該顯示'this.data'的json表示。你確定你在做這個嗎? –

回答

0

當這個功能被稱爲:

OpenReport(report : any) 
{ 
     let list = this.datas.ReportDtDataForMAPP; 
     let newList = list.filter((t) => t.SubReport == report); 
     JSON.stringify(newList) 
     this.myURL = newList[0].URL; 
     this.openUrl(this.myURL); 

} 

數據不加載subscribe是異步:https://angular.io/guide/http

更多

查找異步編程在JavaScript

相關問題