Reputation: 51
I've never seen this one before. This error occurred on both Node.js 6.3.0 and 6.9.1 LTS, which I updated to in an effort to resolve this.
I'm trying to build stats for a game based on some data I have, not particularly important. What is important is that the following function, part of my Game class, fails:
computeStats() {
var stats
, roster
, team, opp, scoreState, oppScoreState
, TOI = this.calculateTOIData()
, eventCounter = this.calculateEventData()
[['home', 'away'], ['away', 'home']].forEach((teams) => { //this is line 74 / error source
team = teams[0];
opp = teams[1];
roster = this[team].roster;
stats = {
//some assignments occur here from my TOI and eventCounter objects
}
this.setStats(team, stats);
})
}
The error thrown is
TypeError: Cannot read property '[object Array]' of undefined
at GameTracker.computeStats (/Users/nawgszy/repo/lib/Game.js:74:5)
at new GameTracker (/Users/nawgszy/repo/lib/Game.js:39:10)
I have no idea how this is possible. The array is hard-coded, right there. Any ideas? I can work around it, but I find this specific structure to be the easiest way to generate stats like I want to use.
Upvotes: 2
Views: 4262
Reputation: 26345
The missing semi-colon after this.calculateEventData()
is causing the following bracket notation to behave as a subscript access, instead of in-place array notation.
The code reads as:
var eventCounter = (this.calculateEventData()[['home', 'away'], ['away', 'home']]).forEach((teams) => { ... });
Note the parenthesis I've added. The comma operator causes ['away', 'home']
to be the subscript, which gets passed through Object.prototype.toString()
, becoming '[object Array]'
.
this.calculateEventData()
returns undefined
. The statements becomes undefined['[object Array]']
.
Basically, use semi-colons, and maybe avoid inline arrays (or prefix them with a semi-colon, because that's safe).
var stats
, roster
, team, opp, scoreState, oppScoreState
, TOI = this.calculateTOIData()
, eventCounter = this.calculateEventData(); // <-- Right there.
A minimal reproduction:
// test.js
const func = () => {};
const result = func()
[[]].forEach(() => {});
Running with Node.js. Will also fail in the browser.
$ node -v
v6.5.0
$ node test.js
/home/foo/test.js:4
[[]].forEach(() => {});
^
TypeError: Cannot read property '[object Array]' of undefined
at Object.<anonymous> (/home/foo/test.js:4:1)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
Upvotes: 2
Reputation: 12993
I think @Oka has worked out the error you are seeing.
However, it looks like have another issue where you are indexing into an object with a string array instead of a string value:
(teams) => {
// teams: string[][];
team = teams[0]; // team: string[];
opp = teams[1]; // opp: string[];
// Issue here: Trying to index `this[team]` with a string array, not a string value.
roster = this[team].roster;
//...
}
Upvotes: 0
Reputation: 5578
I think your problem is that what you showed as [['home', 'away'], ['away', 'home']]
there is actually a variable; and that variable is undefined ( possibly mistyped or undeclared )
Upvotes: 1
Reputation: 1083
It's the ASI that is messing with you I guess. Add a semicolon after eventCounter = this.calculateEventData()
and see how it runs.
more info : http://benalman.com/news/2013/01/advice-javascript-semicolon-haters/
Upvotes: 6