当JSON数据不存在时,ionic2出错

时间:2017-06-15 12:44:15

标签: json typescript ionic-framework ionic2

我制作了一个API,它接收数据库数据并将JSON数据传递给ionic2 app。数据可能包含与用户发送的密钥相关的数据。因此,所有用户可能无法同时存在所有数据,这就是我为所有用户编写公共代码的原因。我的问题是当数据节点不存在于JSON文件中,然后页面断开说“未定义”对象。我该如何解决这个问题?

打字稿方法

displayData()
  {
    if(this.responseDetails[0].DocumentTypeDetails.Deed.Checked)
    this.showDeed= this.responseDetails[0].DocumentTypeDetails.Deed.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.Amend.Checked)
    this.showAmend=this.responseDetails[0].DocumentTypeDetails.Amend.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.MortgageDeed.Checked)
    this.showMortDeed=this.responseDetails[0].DocumentTypeDetails.MortgageDeed.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.MortgageRefi.Checked)
    this.showMortRefi=this.responseDetails[0].DocumentTypeDetails.MortgageRefi.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.MortgageMod.Checked)
    this.showMortMod=this.responseDetails[0].DocumentTypeDetails.MortgageMod.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.Assignment.Checked)
    this.showAssign=this.responseDetails[0].DocumentTypeDetails.Assignment.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.ReleaseSatisfaction.Checked)
    this.showRelSatisfaction=this.responseDetails[0].DocumentTypeDetails.ReleaseSatisfaction.Checked=="True"?1:0;
    if(this.responseDetails[0].DocumentTypeDetails.poa.Checked)
    this.showPOA=this.responseDetails[0].DocumentTypeDetails.poa.Checked=="True"?1:0;
    }

JSON

"DocumentTypeDetails": {
            "PageRec": "AL005",
            "State": "AL",
            "County": "Autauga County",
            "CityTown": null,
            "Zip": null,
            "ShowRecordingInfo": "true",
            "Deed": {
                "Checked": "True",
                "Pages": "1",
                "ConsiderationAmount": "150000"
            },
            "MortgageDeed": {
                "Checked": "False",
                "Pages": null,
                "NewDebtAmount": null
            },
            "MortgageRefi": {
                "Checked": "False",
                "Pages": null,
                "NewDebtAmount": null,
                "OriginalDebt": null,
                "UnpaidDebt": null
            },
            "Assignment": {
                "Checked": "False",
                "Pages": null,
                "Assignments": null
            },
            "ReleaseSatisfaction": {
                "Checked": "False",
                "Pages": null,
                "ReleasesSatisfactions": null
            }
            }

在JSON文件中,我们没有该用户的所有数据,但是对于另一个,可能存在一些丢失的数据。是否有任何检查?

更多到打字稿文件

 private responseUrl='http://localhost:58682/home/index1';

   getresponseDetails(){
        return    this.http.get(this.responseUrl).map(res=>res.json());
    }


     LoadResponseData(){
      this.service.getresponseDetails()
      .subscribe(data=>{
        this.responseDetails=data;
      }

错误

ERROR TypeError: Cannot read property 'Checked' of undefined
    at CalculatePage.displayData (calculate.ts:186)
    at CalculatePage.ngOnInit1 (calculate.ts:111)
    at SafeSubscriber._next (calculate.ts:93)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:234)
    at SafeSubscriber.next (Subscriber.js:183)
    at Subscriber._next (Subscriber.js:125)
    at Subscriber.next (Subscriber.js:89)
    at MapSubscriber._next (map.js:83)
    at MapSubscriber.Subscriber.next (Subscriber.js:89)
    at XMLHttpRequest.onLoad (http.es5.js:1205)

1 个答案:

答案 0 :(得分:0)

为什么不在接收时检查您要查找的节点是否在响应中?

我在考虑这样的事情:

makeGetRequest() {
    this.http.get("https://api.mywebsite.com/users/1454")
    .subscribe(response => {
        let resJson = JSON.parse(response);
        if (resJson['Amend'] === undefined) {
            Observable.throw('no Amend node in received response');
        }
        this.responseDetails = resJson;
    }, error => {
        console.log('oops!');
    });
}
相关问题