无法读取未定义的 属性 '0'(仅在单元测试中)

Cannot read property '0' of undefined (only on unit test)

我不明白为什么会出现以下错误:

Chrome 43.0.2357 (Windows 7 0.0.0) Requests controller test should fill properties from result from xhr requests FAILED TypeError: Cannot read property '0' of undefined at getLastStatus (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/app/tools.js:68:43) at C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/app/requests/requests.js:238:42 at processQueue (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:14792:28) at C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:14808:27 at Scope.$eval (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:16052:28) at Scope.$digest (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:15870:31) at Scope.$apply (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:16160:24) at done (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular/angular.js:10589:47) at handleResponse (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular-mocks/angular-mocks.js:1194:9) at Function.$httpBackend.flush (C:/Users/aazor102115/Desktop/Dev/Balrog/Front/Balrog-UI/bower_components/angular-mocks/angular-mocks.js:1553:26)

此错误仅显示在业力日志中。

但它不应该因为循环条件而未定义。

在 tools.js 文件中:

function getLastStatus(statuses) {
  var lastStatusDate = Date.parse(statuses[0].date); // this is the line pointed in the error
  var lastIndex = 0;

  for (var i = 0; statuses[i]; i++) {
    var currentStatusDate = Date.parse(statuses[i].date);

    if (currentStatusDate > lastStatusDate) {
      lastStatusDate = currentStatusDate;
      lastIndex = i;
    }
  }
  return statuses[lastIndex];
}

在 requests.js 文件中:

Requests.query().$promise.then(function(result) {
      controllerScope.requestsList = [];
      for (var i = 0; result[i]; i++) {
        var currentRequest = {
          id: result[i]["id"],
          projectTitle: result[i]["project"].title,
          status: getLastStatus(result[i]["statuses"]).status, // this is the line pointed in the error
          description: result[i]["description"],
          regions: regionsIdToName(controllerScope.regionsList, result[i]["project"].regions),
          requestDate: moment(result[i]["request_date"]).format("MM-DD-YYYY"),
          developers: result[i]["developers"],
          statusDate: moment(getLastStatus(result[i]["statuses"]).date).format("MM-DD-YYYY")
        };
        controllerScope.requestsList.push(currentRequest);
      }
    });

编辑:getLastStatus(statuses) 函数的第一行添加 console.log(statuses); 时:

一个对象数组显示在浏览器控制台上,就像它应该的那样。

但是Karma控制台显示undefined。所以我不明白是什么原因造成的。

result[i] 不能未定义(由于循环检查)。但似乎 result[i]["statuses"] 未定义。您将 undefined 传递给 getLastStatus,然后尝试获取它的“0”元素。您应该检查 result[i]["statuses"] 是否未定义。

好的,问题来自测试文件。传递给 $httpBackendrespond 方法的预期响应缺少一些属性。