nawgszy nawgszy - 28 days ago 16
Javascript Question

Node.js forEach: cannot read property '[object Array]' of undefined

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.

Answer

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/