• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Javascript] Undefined

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
When I launch my code I get the "ERROR: Cannot read property 'getFullYear' of undefined"

I've managed to track it down:
Code:
if (!isNaN(Date.parse(finishedD)) && finishedD !== undefined){ //I even filter out undefined here, but that did not help as you see.
  _finishedDate = finishedD;
  console.log(this.dueDate + '   ' + finishedD);
  console.log(this.dueDate + '   ' + _finishedDate);
  _isDone = true;
  console.log('pre crash');
  if (!this.inTimeFilter()) {
    _isOverdue = true;
  }
  console.log('aftermath');
}

Which writes out:
Thu Dec 31 2015 00:00:00 GMT+0100 (Västeuropa, normaltid) Wed Mar 02 2016 00:00:00 GMT+0100 (Västeuropa, normaltid)
Thu Dec 31 2015 00:00:00 GMT+0100 (Västeuropa, normaltid) Wed Mar 02 2016 00:00:00 GMT+0100 (Västeuropa, normaltid)
pre crash

So the issue is the !this.inTimerFilter()
Code:
inTimeFilter() {
  console.log(this.dueDate + '   ' + this.finishedDate);
  return this.finishedDate.getFullYear() <= this.dueDate.getFullYear() &&
    this.finishedDate.getMonth() <= this.dueDate.getMonth() &&
    this.finishedDate.getDate() <= this.dueDate.getDate();
}
Which writes:
Thu Dec 31 2015 00:00:00 GMT+0100 (Västeuropa, normaltid) undefined

What. It was fine just before calling the method.
 

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
Okay I debugged further.

I found something useful.
If I make another variable, and make it reference _finishedDate, and then use that one instead of finishedDate, it works.
I have no clue why and it's ugly as fuck.
Code:
_finishedDate = value;
this.test = _finishedDate; //I then use this.test instead of finishedDate
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Your code doesn't show you setting this.finishedDate, which as I am writing I see Aniki asked about.
But in fact, the question need not be asked, you clearly showed you are not setting it with the log message Thu Dec 31 2015 00:00:00 GMT+0100 (Västeuropa, normaltid) undefined.

Just as a small hint for the future, when you want to log multiple objects, preferably don't concatenate them with implicit string conversion.
It's usually a lot more useful to be able to see the actual objects (e.g. in Chrome's devtools).
For example, where you print the dates, I would have done console.log(this.dueDate, this.finishedDate).
This becomes more obvious when you delve into non-native objects (your own classes, etc.), where with string concatenation you will get something along the lines of "[object Object]", which is very unhelpful.

And finally as a side note, both null and undefined evaluate to false, so unless you need to differentiate these cases, you can drop equality tests, e.g. if (object) is the same as if (object !== null && object !== undefined).
 
Last edited:

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
this.finishedDate is a capsuled (is that the right word?) version of _finishedDate to make it private. And since I do set _finishedDate as shown above, this.finished date which refers to _finishedDate, should have the same value, right?
Code:
Object.defineProperty(this, 'finishedDate', {
  get: function() {
    return _finishedDate;
  },
  set: function(value) {
    if (!isNaN(Date.parse(value))) {
      _finishedDate = value;
      this.test = _finishedDate;
      _isDone = true;
      if (!this.inTimeFilter()) {
        _isOverdue = true;
      }
    } else {
      console.log('happens?'); //no this, does not happen.
      _finishedDate = undefined;
    }
  }
});
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
You are returning a global called _finishedDate, which is undefined, as the log message showed. Use this like the rest of the code.
The only reason this.test works is that it is, indeed, using this.

(if you run in a browser environment, for example, then window._finishedDate = value; this.test = window._finishedDate; is what you executed)
 
Last edited:
Status
Not open for further replies.
Top