【问题标题】:Uncaught typeError未捕获的类型错误
【发布时间】:2017-05-26 11:47:56
【问题描述】:

发生了一个不寻常的 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

错误提示

未捕获的类型错误:无法读取未定义的属性“长度”

【问题讨论】:

  • $scope.project.projectMileStones 是什么?它可能没有设置。

标签: javascript jquery


【解决方案1】:

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

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

让我们知道这是否有效!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 它是一个数组,它向我展示了其中存在的对象
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 2014-12-07
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多