繁体   English   中英

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

[英]Error in ionic2 when JSON data is not present

我创建了一个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文件中,我们没有该用户的所有数据,但对于另一个,可能存在一些丢失的数据。是否有任何检查?

更多到Typescript文件

 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)

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

我在考虑这样的事情:

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!');
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM