简体   繁体   中英

Error in ionic2 when JSON data is not present

I made an API which takes db data and passes JSON data to ionic2 app.The data may have data accordingly to what key is sent by the user.So all the data may not be present for all users at the same time and that's why i have written a common code for all the users. My issue is when data node is not present in JSON file then the page breaks saying "undefined" object. How do i solve this?

Typescript Method

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
            }
            }

Here in JSON file we don't have all the data for this user but for another one some of the missing data might be present.Is there any check for this?

More to Typescript file

 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

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)

Why don't you check if the node you are looking for is in the response as you receive it?

I'm thinking of something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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