2016-07-16 6 views
-2

私は、ドロップダウンメニューを持っている、と私は「すべて」オプションを選択すると、それは私のコンソール上でこのエラー与える:未定義のプロパティ '0'を読み取ることができません - javascriptエラー?

TypeError例外を:プロパティを読み取ることができません「0」未定義 のn個で$ scope.onSearchByChanged

$scope.loadEditorGroups = function() { 
      Reference.getEditorGroups($scope, function (response) { 
       $scope.filterScope.editorGroups = response.list; 
       if ($scope.filterScope.editorGroups.length > 0) { 
        $scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id 
       } 
      }); 
     }; 

ここ
$scope.onSearchByChanged = function() { 
      if ($scope.filter.list.searchBy == 'DEPARTMENT_CODE' && !$scope.filterScope.departments) { 
       $scope.loadDepartments(); 
      } else if ($scope.filter.list.searchBy != 'DEPARTMENT_CODE') { 
       $scope.filter.list.departmentId = 0; 
      } 
      if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) { 
       $scope.loadEditorGroups(); 
      } else if ($scope.filter.list.searchBy != 'GROUP') { 
       $scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; //line 70 
      } 
      $scope.clearFilter('text'); 
     }; 
     if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) { 
      $scope.loadEditorGroups(); 
     } 
     if ($scope.filter.list.searchBy == 'DEPARTMENT_CODE' && !$scope.filterScope.departments) { 
      $scope.loadDepartments(); 
     } 
     $scope.isStatusSelected = function (status) { 
      return _.indexOf($scope.filter.list.talentAssignmentStatuses, status) > -1; 
     }; 
     $scope.selectTalentAssignmentStatus = function (status) { 
      $scope.clearFilter('text'); 
      if ($scope.isStatusSelected(status)) { 
       _.remove($scope.filter.list.talentAssignmentStatuses, function (el) { 
        return status == el; 
       }); 
      } else { 
       $scope.filter.list.talentAssignmentStatuses.push(status); 
      } 
     }; 

がloadEditorGroups機能である:(http://localhost:8080/js/jenkinsVersion/directives/assignment-filter.js:70:81

だから、私は私のスクリプト、関数、行70、文字81に行ってきました

私はまだJSを学んでいます。このエラーはなぜ投げられるのですか?そのエディタグループリストから取得したいアイテムの値を変更すると、同じエラーが表示されますが、対応する番号が表示されます。あなたの助けに感謝します。私がさらなる情報を提供できるかどうか私に知らせてください。ありがとうございました!

+0

おそらく '$ scope.filterScope.editorGroups'は空の配列なので、最初の配列項目を読み込もうとすると未定義です。 – Oriol

+0

70:81は途中で行70、列81を意味します。 81行目は関係ありません。 – Jacob

+1

'$ scope.filterScope.editorGroups'は' undefined'です。要素 '' 0 'にアクセスできません。 – ccjmne

答えて

1

このif-elseブロックのためのあなたのロジックはおそらく間違っている:両方とき

if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) { 
    $scope.loadEditorGroups(); 
} else if ($scope.filter.list.searchBy != 'GROUP') { 
    $scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; //line 70 
} 

プログラムはelse ifブロックを入力します:

  • if条件がfalseあり、それは次のようになります。 ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) == false
  • t彼else if条件はつまり、trueです:($scope.filter.list.searchBy != 'GROUP') == true

我々は、これら2つの文を取り、それらを簡素化することができます。

ステップ2では
  1. !($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) && ($scope.filter.list.searchBy != 'GROUP')
  2. ($scope.filter.list.searchBy != 'GROUP' || $scope.filterScope.editorGroups) && ($scope.filter.list.searchBy != 'GROUP')
  3. ($scope.filter.list.searchBy != 'GROUP')

、私はDe Morgan's lawをシンプルに適用したify !(A && B)(!A || !B)
手順3では、(A || B) && AがちょうどAと同じであるため、&&を簡略化しました。

実際には、私たちがelse ifブロックを入力したときに知っていることはすべて、searchBy != 'GROUP'です。 何かeditorGroupsとわからないのですが、確かにundefined!あなたはおそらくがため探しているもの

は次のとおりです。

if ($scope.filter.list.searchBy == 'GROUP' || !$scope.filterScope.editorGroups) { 
    $scope.loadEditorGroups(); 
} else if ($scope.filter.list.searchBy != 'GROUP') { 
    $scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; 
} 

お知らせif状態で||。これによりelse if($scope.filter.list.searchBy != 'GROUP' && $scope.filterScope.editorGroups)のときにのみ実行されるので、editorGroups[0]はエラーになりません。これがあなたのものであるなら私は十分にわかりません私は間違っているときにこのコードを実行してください。:-)

+0

偉大な答え、ありがとう。 – keslami

0

70:81は、行70、文字81を意味します。

問題がeditorGroups[0]である:editorGroupsundefinedある場合undefined0と呼ばれるプロパティを持たないので、それはeditorGroups[0]を読み取ることができません。

editorGroupsが未定義でないことを確認してください。

関連する問題