[英]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.