【问题标题】:How to catch error: Uncaught TypeError: Cannot read property 'subjects' of undefined如何捕获错误:未捕获的类型错误:无法读取未定义的属性“主题”
【发布时间】:2018-01-29 21:28:29
【问题描述】:

我正在尝试从数据库中读取时间表,但如果星期一是公共假期,我会收到错误 Uncaught TypeError: Cannot read property 'subjects' of undefined

我正在调用'subject' 的 JQuery 行是:

var currentsubjectone = timetableday.periods[index].subjects[0],
    currentsubjecttwo = timetableday.periods[index].subjects[1];

被调用的函数: 函数 updateTeacherTimetableClassDetails(timetableday, periodslots, date) { var lastmondayfordate = getLastMonday(日期);

periodslots.each(function(index){

    // Testing
    if (typeof timetableday.periods[index].subjects[0] === undefined ){
        timetableday.periods[index].subjects[0] == '*';
    }
    if (timetableday.periods[index].subjects[1] === undefined ){
        timetableday.periods[index].subjects[1] == '*';
    }
    //testing

    var currentsubjectone = timetableday.periods[index].subjects[0],
        currentsubjecttwo = timetableday.periods[index].subjects[1];

    var lineindentifier = [];
        subjectcode = [];
        roomcode = [];


    if (currentsubjectone != undefined)
    {
        lineindentifier.push(currentsubjectone.lineidentifier);
        subjectcode.push(currentsubjectone.subjectcode);
        roomcode.push(currentsubjectone.roomcode);
    }
    if (currentsubjecttwo != undefined)
    {
        lineindentifier.push(currentsubjecttwo.lineidentifier);
        subjectcode.push(currentsubjecttwo.subjectcode);
        roomcode.push(currentsubjecttwo.roomcode);
    }
    var classdetail = $(this).find('.class-detail');
    classdetail.empty();
    classdetail.append($('<div class="line-identifier">' + lineindentifier.join('<br />') + '</div><div class="subject-code">' + subjectcode.join('<br />') + '</div><div class="room-code">' + roomcode.join('<br />') + '</span></div>'));

    var perioddom = this;
    $(perioddom).find('.attendance-checklist').empty();
    console.log(subjectcode);
    // add in attendance checklist if there is a valid subject
    if (subjectcode.length > 0)
    {
        // attendance checklist might not be available, so we build some code to run when it is
        loadAttendanceChecklistAndRunFunction(g_loginkey, g_selectedteacher.teachercode, g_selectedyear, function() {
            var currentweekchecklist = g_teacherattendancechecklist.weeks[lastmondayfordate.getTime()],
                checklist = undefined;

            if (currentweekchecklist != undefined && currentweekchecklist.days.length > 4)
            {
                checklist = currentweekchecklist.days[date.getDay() - 1];

                if (checklist.checklist[index] == 'Y')
                    updateTeacherTimetableAttendanceChecklist(index + 1, true, perioddom, date);
                else
                    updateTeacherTimetableAttendanceChecklist(index + 1, false, perioddom, date);
            }
            else
            {
                updateTeacherTimetableAttendanceChecklist(index + 1, false, perioddom, date);
            }
        });
    }
});

}

当我加载页面时,它在星期一不是公共假期并且有数据时效果很好,但是当星期一是公共假期时,没有数据被发送,因此它会生成 TypeError

我尝试过的:typeof(在上面的代码中)并检查 null。

任何指导都会很棒。

谢谢!

【问题讨论】:

  • subjects 未定义时不能做subjects[0]
  • 所以我需要先定义一个空白数组?
  • 可能类似于:if(Array.isArray(subjects) &amp;&amp; subjects.length)
  • 那不是currentsubjectone != undefined @charlietfl 吗?
  • No.. 试图建议您首先检查 subjects 是什么

标签: jquery jquery-mobile


【解决方案1】:

我找到了我的问题的答案。我得到的错误不是由 jQuery 代码引起的,而是由服务器发送的 XML 引起的。发送的数据丢失,因此网站试图读取 XML 中的日期标签作为主题的一部分。

【讨论】:

    猜你喜欢
    • 2018-01-04
    • 2017-02-11
    • 1970-01-01
    • 2019-02-26
    • 2017-07-26
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多