我做了一个API,它获取数据库数据并将JSON数据传递给ionic2应用程序。数据可能具有与用户发送的密钥相对应的数据。因此,并非所有数据都同时存在于所有用户中,这就是我为所有用户编写通用代码的原因。我的问题是当 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;
}
杰伦
"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)
为什么不检查您要查找的节点是否在收到响应时在响应中?
我在想这样的事情:
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!');
});
}