2

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.

4
  • I have no errors here. Can you update your post with full code? Commented Nov 11, 2016 at 1:44
  • Is it really the full code you're sharing? This works OK on chrome...: [['home', 'away'], ['away', 'home']].forEach((teams) => { console.log(teams)}); Commented Nov 11, 2016 at 1:48
  • another notice - you have 2 different team vars... Commented Nov 11, 2016 at 1:50
  • what version of node are you using? there's definitely no problem with forEach, see this: runkit.com/skhavari/40539854 Commented Nov 11, 2016 at 1:55

4 Answers 4

6

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Beautiful, thanks. I guess that's a flaw with my style of writing my variables, will have to be careful with that in the future.
yea, you probably seen lots of js like ;(function(){..})() or ;[1,2,3].forEach..
2

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

1 Comment

This is a great answer, I accepted the other one first but maybe I chose rashly. Thanks for the help and the clear explanation.
1

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 )

Comments

0

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;

    //...
}

2 Comments

Thanks for the shoutout, but I believe you're slightly mistaken. teams is a string[] - team is a string. [['foo', 'bar'], ['bar', 'foo']].forEach((array) => console.log(typeof array[0])) --> (2) string
As Oka said, I'm iterating over the two-dimensional array, so teams is, at every point, a string[], and thus teams[i] is a string. It works now that I put in a semicolon as suggested!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.