-1

The following code is throwing an "Undefined is not a function" error at the point indicated.

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."];
        }

This code is part of a jqGrid table, and is using momentjs for the date objects. I can create the same momentjs objects and copy/paste the same 'if' statement to the JS console, and it executes fine. Further, the age/minage/maxage test preceding it works perfectly. There is something wrong with this particular context, and I can't figure out what it is.

Does anyone see what is wrong?

wedgef5
  • 81
  • 2
  • 8

1 Answers1

0

I think instead of isEqual, you need isSame

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

chris van hooser
  • 388
  • 3
  • 11
  • To clarify a little more, when you get "undefined is not a function", it is meaning the function you are trying to execute is undefined, not the property that contains the function, so thats what made me look up the function names on momentjs – chris van hooser Apr 01 '15 at 04:40
  • Wow...I feel dumb. I think isEqual got copied from the pseudo-code I was using. What I don't understand is why this didn't throw an error from the JS console. Thank you for catching it! – wedgef5 Apr 01 '15 at 11:37