【问题标题】:Angular : Error accessing json objectAngular:访问 json 对象时出错
【发布时间】:2017-07-05 20:28:07
【问题描述】:

在我的控制器内部,我有一个 $http.get() 函数,在该 $http 内部,我有以下代码行 -

248            console.log($scope.ticketAPIData);
249
250            var j = 0;
251            while(j < response.data.d.results.length){
252              console.log($scope.ticketAPIData[j].data.Login_ID);
253              j++;
254            }

第 248 行的第一个 console.log 生成的数据可以在控制台中展开,但如果我在 while 循环中使用相同的范围变量并尝试访问单个数据元素(第 252 行),则会出现错误。

Chrome 控制台错误 -

Array[0]
    0: Object
        config: Object
        data: Object
            First_Name: "Rahul"
            Full_Name: "Rahul A Parelkar"
            Login_ID: "**********"
            __proto__: Object
        headers: (d)arguments: (...)caller: (...)length: 1name: ""prototype: Object__proto__: ()[[FunctionLocation]]: angular.js:9518[[Scopes]]: Scopes[3]
        status: 200
        statusText: "OK"__proto__: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    9: Object
    10: Object
    11: Object
    12: Object
    13: Object
    14: Object
    15: Object
    16: Object
    17: Object
    18: Object
    length: 19
    __proto__: Array[0]

angular.js:12520 TypeError: Cannot read property 'data' of undefined
    at app.js:252
    at angular.js:14792
    at r.$eval (angular.js:16052)
    at r.$digest (angular.js:15870)
    at r.$apply (angular.js:16160)
    at g (angular.js:10589)
    at T (angular.js:10787)
    at XMLHttpRequest.w.onload (angular.js:10728)

完成 $http -

 $http.get('https://path/ListData.svc/TeamList').
  then(function(res) {
      //shifting the response from rest call(res) into local variable(response)
      var response = res;
      $scope.evolveTeam = [];
  var i = 0;
  var lanID = [];
  var domain = [];
  var displayPicture = [];
  var jobTitle = [];
  var searchName = [];
  var apiURL = [];
  var userData = [];
  var imageURL = [];
  var imageData = [];
  $scope.ticketAPIData = [];

  while (i < response.data.d.results.length) {

      searchName.push(response.data.d.results[i].Domain + response.data.d.results[i].LanID);
      lanID.push(response.data.d.results[i].LanID);
      domain.push(response.data.d.results[i].Domain);
      jobTitle.push(response.data.d.results[i].Job_title);
      displayPicture.push(response.data.d.results[i].Display_picture);
      //var deferred = $q.defer();
      apiURL.push('https://path/api/user/');
      imageURL.push('https://path/Profile%20Pictures/');
      userData.push(response.data.d.results[i].Domain + '_' + response.data.d.results[i].LanID);
      imageData.push(response.data.d.results[i].Domain + '_' + response.data.d.results[i].LanID + '_LThumb.jpg'); 
      response.data.d.results[i].DomainLanID = (response.data.d.results[i].Domain + '\\' + response.data.d.results[i].LanID).toLowerCase();
      //SP list returns the image URL with caption in the same string so removing
      //everything after the http://../../abc.jpg
      if (response.data.d.results[i].Display_picture) {
          //console.log(displayPicture[j]);
          var s = response.data.d.results[i].Display_picture.toString();
          var n = s.indexOf(',');
          s = s.substring(0, n != -1 ? n : s.length);
          response.data.d.results[i].Display_picture = s;
      }


      $http.get('https://path/api/user/' + response.data.d.results[i].Domain + '_' + response.data.d.results[i].LanID, {
          withCredentials: true
      }).then(
          function(successResponse) {

              $scope.ticketAPIData.push(successResponse);
          },
          function(failedResponse) {});
      */

      i++;
  }




  console.log(response.data.d.results);

  console.log($scope.ticketAPIData);

  var j = 0;
  while (j < response.data.d.results.length) {
      console.log($scope.ticketAPIData[j].data.Login_ID);
      j++;
  }




 });

【问题讨论】:

  • 发布 http.get() 方法

标签: angularjs json ajax http


【解决方案1】:

我的理解:

根据app.js 中行号252 的控制台错误

TypeError: 无法读取未定义的属性“数据”

您正在访问对象$scope.ticketAPIData[j]data 属性,即undefined。在这里,j = 0&lt; response.data.d.results.length

观察:

  • 如果response.data.d.results.length 大于$scope.ticketAPIData 数组的长度,则会出现此错误。

演示

var obj = [
  {
  "id":1,
  "name":"alpha"
  },
    {
  "id":1,
  "name":"beta"
  },
    {
  "id":1,
  "name":"gama"
  }
];

var j = 0;
while(j < 5) {
console.log(obj[j].name);
j++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2022-07-20
    • 2016-03-11
    • 2023-02-07
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多