2017-02-16 34 views
0

裏面我的控制器我有一個$ http.get()函數和$ HTTP內我有以下代碼行 -角:錯誤訪問JSON對象

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++; 
    } 




}); 
+0

張貼http.get()方法 –

回答

0

如果我假設你正確的話,你的while循環改變

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

希望它會工作。

+0

不工作。錯誤不在while狀態。我可以硬編碼長度仍然控制檯給出相同的錯誤。錯誤在這部分 - $ scope.ticketAPIData [j] .data.Login_ID –

0

我的理解:

按上線數252控制檯錯誤app.js

TypeError: Cannot read property 'data' of undefined

您正在訪問的對象$scope.ticketAPIData[j]undefineddata財產。這裏,j = 0< response.data.d.results.length

觀察:

  • 如果response.data.d.results.length是大於所述$scope.ticketAPIData數組的長度,然後將發生該錯誤。

DEMO

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++; 
 
}