首页 > 解决方案 > 无法访问 JSON 获取错误

问题描述

我无法访问 json。PFB json

我正在尝试访问:-

this.businessSwitchName = d.businessSwitchDetails.nodeName;

并尝试通过插值绑定到 html

var d:any = {
    "service": "091PUNE623017759708",
    "routerDetails": {
        "nodeName": null,
        "portName": null,
        "nodeIP": null,
        "nodeID": null
    },
    "agsDetails": {
        "nodeName": null,
        "portName": null,
        "nodeIP": null,
        "nodeID": null
    },
    "businessSwitchDetails": {
        "nodeName": "Dyaneshwar Park",
        "portName": null,
        "nodeIP": "10.171.4.3",
        "nodeID": "365429"
    },
    "handoffPort": null,
    "qosLoopingPort": null
}

但收到此错误:

这个错误

ERROR TypeError: Cannot read property 'businessSwitchDetails' of undefined
    at ContactFormComponent.push../src/app/contact-form/contact-form.component.ts.ContactFormComponent.onChange (contact-form.component.ts:16)
    at Object.eval (ContactFormComponent.html:48)
    at handleEvent (core.js:9953)
    at callWithDebugContext (core.js:11046)
    at Object.debugHandleEvent [as handleEvent] (core.js:10749)
    at dispatchEvent (core.js:7415)
    at core.js:7859
    at HTMLInputElement.<anonymous> (platform-browser.js:1140)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:3662)

标签: jsonangular

解决方案


我无法发表评论,因此需要在答案中执行此操作,但您需要显示更多信息。

您在哪里初始化变量 d,因为看起来 d 与 businessSwitchName 不在同一范围内


您在尝试使用变量 d 后对其进行初始化。这意味着当您尝试执行以下操作时:

this.businessSwitchName = d.businessSwitchDetails.nodeName;

变量 d 仍然是未定义的,所以你应该先定义 d 然后再用它做点什么。

所以让你的 onChange() 方法是这样的:

onChange(){
var d = {"service": "091PUNE623017759708",
"routerDetails": {
    "nodeName": null,
    "portName": null,
    "nodeIP": null,
    "nodeID": null
},
"agsDetails": {
    "nodeName": null,
    "portName": null,
    "nodeIP": null,
    "nodeID": null
},
"businessSwitchDetails": {
    "nodeName": "Dyaneshwar Park",
    "portName": null,
    "nodeIP": "10.171.4.3",
    "nodeID": "365429"
},
"handoffPort": null,
"qosLoopingPort": null};


  this.businessSwitchName = d.businessSwitchDetails.nodeName;
  this.businessSwitchIp = d.businessSwitchDetails.nodeIP;
  this.portName = d.businessSwitchDetails.portName;

  let da = {
  "serviceAttribute": {
    "serviceType": "GVPN",
    "category": "category",
    "name": "091PUNE623017759708"
  },
  "btsIp": "10.171.4.3"
};

  console.log(da);

  this.service.portService(da).
    subscribe (response => {
        console.log(response);

    });  
}

}

推荐阅读