想不通 JavaScript Undefined is not a function error

Can't figure out JavaScript Undefined is not a function error

以下代码在指示的位置抛出 "Undefined is not a function" 错误。

beforeSubmit: function(data, formid) {
            data.classid = data.classname; // Copy classname value to classid
            data.studentid = data.studentname; // Copy studentname value to studentid
            var studentRows = $studentTable.jqGrid('getRowData'),
                classRows = $classTable.jqGrid('getRowData'),
                enrollRows = $enrollTable.jqGrid('getRowData'),
                idx, dob, age, minage, maxage, stime, etime, st, et;
            for (idx in studentRows) {
                if (studentRows[idx].studentid === data.studentid) {
                    dob = studentRows[idx].dob;
                    break;
                }
            }
            if (typeof dob != "undefined") {
                age = calcAge(dob);
            }
            for (idx in classRows) {
                if (classRows[idx].classid === data.classid) {
                    minage = parseInt(classRows[idx].minage);
                    maxage = parseInt(classRows[idx].maxage);
                    stime = moment(coopStartDate.format('YYYY-MM-DD')+' '+classRows[idx].startTime, 'YYYY-MM-DD h:mm a');
                    etime = stime.clone().add(classRows['classMinutes'], 'minutes');
                    break;
                }
            }

            if (typeof age != "undefined" && typeof minage != "undefined" && typeof maxage != "undefined") {
                if (age < minage || age > maxage) {
                    return [false, "Your student is "+age+". The minimum age for this class is "+minage+", and the maximum age is " + maxage];
                }
            }

            if (typeof stime != "undefined" && typeof etime != "undefined") {
                for (idx in enrollRows) {
                    if (enrollRows[idx].studentid === data.studentid) {
                        st = moment(coopStartDate.format('YYYY-MM-DD')+' '+enrollRows[idx].startTime, 'YYYY-MM-DD h:mm a');
                        et = st.clone().add(enrollRows[idx].classMinutes, 'minutes');
                        // ERROR THROWN ON NEXT LINE
                        if ((stime.isBefore(et) || stime.isEqual(et)) && (etime.isAfter(st) || etime.isEqual(st))) {
                            return [false, "This student is already in a class at the time of the selected class."];
                        }
                    }
                }
            }
            return [false, "Something bad happened."];
        }

此代码是 jqGrid table 的一部分,并且将 momentjs 用于日期对象。我可以为 JS 控制台创建相同的 momentjs 对象和 copy/paste 相同的 'if' 语句,并且它执行得很好。此外,它之前的 age/minage/maxage 测试完美运行。这个特定的上下文有问题,我无法弄清楚它是什么。

有没有人看出哪里出了问题?

我认为你需要 isSame

而不是 isEqual

http://momentjs.com/docs/#/query/is-same/