1
votes

I don't understand why I'm getting the following error :

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)

This error is showed only on the karma logs.

But it shouldn't be undefined because of a loop condition.

In the tools.js file :

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

In the requests.js file :

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

EDIT : When adding console.log(statuses); at the first ine of the getLastStatus(statuses) function :

An array of object is displayed on the browser console, like it should.

But undefined is displayed on the Karma console. So I don't get what is causing it.

2

2 Answers

1
votes

result[i] can't be undefined (due to the loop check). But it seems result[i]["statuses"] is undefined. You pass undefined to getLastStatus and then try to get it's '0' element. You should check whether result[i]["statuses"] is undefined.

1
votes

Ok, the issue came from the test file. The expected response passed to the respond method of $httpBackend was missing some attributes.