Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当JSON数据不存在时,ionic2中出现错误_Json_Typescript_Ionic Framework_Ionic2 - Fatal编程技术网

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

当JSON数据不存在时,ionic2中出现错误,json,typescript,ionic-framework,ionic2,Json,Typescript,Ionic Framework,Ionic2,我制作了一个API,它接收db数据并将JSON数据传递给ionic2应用程序。数据可能包含与用户发送的密钥相应的数据。因此,所有数据可能不会同时出现在所有用户身上,这就是为什么我为所有用户编写了一个通用代码。我的问题是,当JSON文件中不存在数据节点时,页面会显示“未定义”对象。如何解决此问题? 打字脚本方法 displayData() { if(this.responseDetails[0].DocumentTypeDetails.Dedit.Checked) this.showDect=th

我制作了一个API,它接收db数据并将JSON数据传递给ionic2应用程序。数据可能包含与用户发送的密钥相应的数据。因此,所有数据可能不会同时出现在所有用户身上,这就是为什么我为所有用户编写了一个通用代码。我的问题是,当JSON文件中不存在数据节点时,页面会显示“未定义”对象。如何解决此问题?

打字脚本方法

displayData()
{
if(this.responseDetails[0].DocumentTypeDetails.Dedit.Checked)
this.showDect=this.responseDetails[0]。DocumentTypeDetails.Dect.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.MortgageDedit.Checked)
this.showmortdect=this.responseDetails[0]。DocumentTypeDetails.MortgageDect.Checked==“True”?1:0;
if(this.responseDetails[0].DocumentTypeDetails.MortgageRefi.Checked)
this.showMortRefi=this.responseDetails[0]。DocumentTypeDetails.MortgageRefi.Checked==“True”?1:0;
如果(此.responseDetails[0].DocumentTypeDetails.MortgageMod.Checked)
this.showMortMod=this.responseDetails[0]。DocumentTypeDetails.MortgageMod.Checked==“True”?1:0;
if(this.responseDetails[0].DocumentTypeDetails.Assignment.Checked)
this.showsign=this.responseDetails[0]。DocumentTypeDetails.Assignment.Checked==“True”?1:0;
if(this.responseDetails[0].DocumentTypeDetails.ReleaseSumption.Checked)
this.showrelsatification=this.responseDetails[0]。DocumentTypeDetails.ReleaseScification.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”,
“国家”:“AL”,
“县”:“奥托加县”,
“城市镇”:空,
“Zip”:空,
“ShowRecordingInfo”:“true”,
“契约”:{
“选中”:“正确”,
“页面”:“1”,
“对价金额”:“150000”
},
“抵押契据”:{
“选中”:“假”,
“页面”:空,
“新债务金额”:空
},
“MortgageRefi”:{
“选中”:“假”,
“页面”:空,
“新债务金额”:空,
“OriginalDebt”:空,
“未支付债务”:空
},
“转让”:{
“选中”:“假”,
“页面”:空,
“赋值”:空
},
“释放满意度”:{
“选中”:“假”,
“页面”:空,
“释放消息”:null
}
}
在JSON文件中,我们没有此用户的所有数据,但对于另一个用户,可能存在一些缺少的数据。是否对此进行检查

有关键入脚本文件的更多信息

private responseUrl=http://localhost:58682/home/index1';
getresponseDetails(){
返回this.http.get(this.responseUrl.map)(res=>res.json());
}
LoadResponseData(){
this.service.getresponseDetails()
.订阅(数据=>{
this.responseDetails=数据;
}
错误

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

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

我在想这样的事情:

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

但是如果它在检查时抛出错误,它什么时候会加载所有剩余的数据?我不知道你的意思。你能编辑你的帖子,并在JSON数据“修正”中包含一个正确响应和错误响应的例子,指出第二种情况下的问题是什么吗不存在这就是为什么当我尝试从中获取.checked数据时,会抛出未定义的错误…现在我通过临时修复解决了问题…检查每个元素是否未定义…这工作正常,但不是一个好方法检查我更新的代码上面的
if(resJson['Amend']==未定义){…}
但是如果它在检查时抛出错误,它什么时候会加载所有剩余的数据?我不确定你的意思。你能编辑你的帖子,并在JSON数据“修正”中包含一个正确响应和错误响应的例子,指出第二种情况下的问题是什么吗不存在这就是为什么当我试图从中获取.checked数据时,会抛出未定义的错误…现在我用临时修复程序解决了问题…检查了每个元素是否未定义…这工作正常,但不是一个好方法检查我更新的代码上面的
if(resJson['Amend']==未定义){…}