未捕获的typeError

时间:2017-05-26 11:47:56

标签: javascript jquery

发生异常的javascript错误有人可以帮我解决这个问题吗?这是我的全部功能

$scope.getAllSubMilestones = function() {
    if (typeof $scope.subMilestones == 'undefined' || $scope.subMilestones.length < 0 || $scope.subMilestones == '') {
        // the array is defined and has at least one element
        var URL = $window.sessionStorage.getItem('HOST_URL') + '/subMilestoneFormObjs';

        function onSuccess(response) {
            $scope.subMilestones = response.data;
            pu_ws_counter++;
            setTimeout(function() {

                for (var i = 0; i < $scope.subMilestones.length; i++) {
                    var sub_milestone = $scope.subMilestones[i];

                        for(var k = 0; k < $scope.project.projectMileStones.length; k++) {
                            var pm = $scope.project.projectMileStones[k];
                            var selected_sub_milestone = pm.projectSubMileStones;

                        for (var j = 0; j < selected_sub_milestone.length; j++) {
                            var s = selected_sub_milestone[j];
                            if (sub_milestone.id == s.subMilestone.id) {
                                //document.getElementById('#' + sub_milestone.id).checked = true;
                                //                                                $('#' + sub_milestone.id).prop('checked', true);
                                $('#' + sub_milestone.id).click();
                            }
                        }
                        }
                    }

            }, 1000);
            $("#mydiv").hide();

        };

显示错误的行是

  

for(var k = 0; k&lt; $ scope.project.projectMileStones.length; k ++)

错误说

  

未捕获的TypeError:无法读取未定义的属性“长度”

2 个答案:

答案 0 :(得分:0)

如果if (typeof $scope.subMilestones == 'undefined' || $scope.subMilestones.length < 0 || $scope.subMilestones == '') {true,您的$scope.subMilestones会返回undefined,但您的评论下面指出如果已定义,则应返回true。尝试将if语句更改为以下内容:

if (!(typeof $scope.subMilestones == 'undefined' || $scope.subMilestones.length < 0 || $scope.subMilestones == '')) {

如果有效,请告诉我们!

答案 1 :(得分:0)

尝试在控制台中打印 $ scope.project.projectMileStones 。 对象$ scope.project很可能没有 projectMileStones 属性或 projectMileStones 不是类型数组。