如何捕获错误:未捕获的TypeError:无法读取属性' subject'未定义的

时间:2018-01-29 21:28:29

标签: jquery jquery-mobile

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

我调用'subject'的JQuery行是:

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

正在调用的函数: function updateTeacherTimetableClassDetails(timetableday,periodslots,date){     var lastmondayfordate = getLastMonday(date);

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。

任何指导都会很棒。

谢谢!

1 个答案:

答案 0 :(得分:0)

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