PDA

View Full Version : Resolved Object Undefined Error



championcyclones
02-12-2012, 10:43 PM
I'm working on creating a lunar calendar and am encountering an undefined object error from functions called from an external js file.

Here is the exact error message:

"Uncaught TypeError: Cannot call method 'getFullYear' of undefined"

I am relatively new to javascript and was hoping someone could help me out with it?

jscheuer1
02-12-2012, 11:32 PM
If all you say is an accurate description of where this error is coming from, it most likely means that somewhere in that external file there is something like:


something.getFullYear()

where something is a variable that is expected to represent a Date Object, but that at that point in the code for some reason does not.

That could be because it was fed faulty data, or because it is poorly written, or a bit of both. There could possibly be another reason or reasons. I can't tell for sure what or how to fix it without seeing the live page where this is happening.

Even then, if all or some of the code contributing to the error is too incoherent, the fix might not be all that obvious. But I'm willing to have a look.

If you want more help, please post a link to the page on your site that contains the problematic code so we can check it out.

championcyclones
02-13-2012, 02:08 AM
Here's the first function that I experience the error with from the external js file:



function calcMAge(cDay) {
var thisYear=cDay.getFullYear();
var thisMonth=cDay.getMonth()+1;
var thisDay=cDay.getDate();

var YY = thisYear - Math.floor((12 - thisMonth)/10);
var MM = thisMonth+9;
if (MM >= 12) MM=MM-12;

var K1 = Math.floor( 365.25 * ( YY + 4712 ) );
var K2 = Math.floor( 30.6 * MM + 0.5 );
var K3 = Math.floor( Math.floor( ( YY / 100 ) + 49 ) * 0.75 ) - 38;

var JD = K1 + K2 + thisDay + 59;
if (JD > 2299160) JD=JD-K3;

var IP = normalize((JD-2451550.1)/29.530588853);
var AG = IP*29.53; // the moon's age in days

return roundValue(AG, 3);
}


Am I correct in assuming that this function is calling for the variable cDay from the original document?

jscheuer1
02-13-2012, 03:34 AM
It could be. It is using whatever was passed to it as cDay. That could have happened on the original document. By the way, what do you mean by "original document"? Is that the page that is using this external file?

In any case the calcMAge function expects to be given a valid Date Object which it then processes internally as cDay, and appears to use that to return the "Age of the Moon" on that date.

So to get the current "Age of the Moon", one could do:


alert(calcMAge(new Date()));

If you had some other date in mind - say, May 1st 1960, one could do:


alert(calcMAge(new Date(1960, 4, 1)));

or:


alert(calcMAge(new Date("5/1/1960")));

or:


alert(calcMAge(new Date("May 1 1960")));

Again though, I cannot tell where or how it's messing up without seeing the page where this is a problem.

But it looks like the calcMAge function is OK at least (I won't vouch for its accuracy, simply that it appears workable* if fed a valid Date Object), so now it's most likely the problem is that it's being fed faulty data - something other than a Date Object.

If you want more help, please post a link to the page on your site that contains the problematic code so we can check it out.



*However, it appears to require the assistance of two other functions (normalize** and roundValue), which are not native functions in javascript. Hopefully these are available to it, otherwise it will not function.



**normalize is a native javascript function, but not as it's used here.

championcyclones
02-17-2012, 03:55 PM
This has been resolved.