Saqueib
Saqueib

Reputation: 3520

TypeError: Cannot read property in but property is present

I have a weird issue TypeError: Cannot read property 'allowed' of undefined on last element in $scope.leaveType.

Update :

Thanks @dusky its fixed the problem, but now am getting below errors in console, but app is working here is screenshot

Full code of controller is here

its seems its not getting $scope.myLeaves[id]

$scope.myLeaves = { "1": 18, "2": 12, "3": 0, "4": 0, "5": 1, "6": 1, "7": 1};

enter image description here

Please help what i have missed here.

  $scope.leaveType = [
    { "id": "1", "name": "Paid Leave", "allowed": 18},
    { "id": "2", "name": "Caual Leave", "allowed": 12},
    { "id": "3", "name": "Maternity Leave", "allowed": 0},
    { "id": "4", "name": "Paternity Leave", "allowed": 0},
    { "id": "5", "name": "Celebration Leave", "allowed": 1},
    { "id": "6", "name": "Compensatory Off","allowed": 1},
    { "id": "7", "name": "Optional Holiday Leave", "allowed": 1}
  ];

  $scope.myLeaves = { "1": 18, "2": 12, "3": 0, "4": 0, "5": 1, "6": 1, "7": 1};

  $scope.taken  = function (index) {
        var allowed = $scope.leaveType[index].allowed,
            leaveTypeId = $scope.leaveType[index].id,
            taken = (parseFloat(allowed,10)) - (parseFloat($scope.myLeaves[leaveTypeId], 10));

        return taken;
  }
    //Get remaining leaves in a category
  $scope.remaining  = function (index) {
        var allowed = $scope.leaveType[index].allowed,
            leaveTypeId = $scope.leaveType[index].id,
            remain = (parseFloat(allowed,10)) - $scope.taken(leaveTypeId, allowed);

        return remain;
  }

Here is Plunker

Upvotes: 0

Views: 250

Answers (1)

dusky
dusky

Reputation: 1313

Your remaining function is wrong. $scope.taken takes the index as argument. By the way: parseFloat doesn't take 2 arguments (parseInt does). And it's not needed here as allowed is already a number.

The error is on this line:

remain = (parseFloat(allowed,10)) - $scope.taken(leaveTypeId, allowed);

It should be:

remain = allowed - $scope.taken(index);

So it becomes:

  $scope.remaining  = function (index) {
        var allowed = $scope.leaveType[index].allowed,
            remain = allowed - $scope.taken(index);

        return remain;
  }

Upvotes: 1

Related Questions