最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js forEach: cannot read property '[object Array]' of undefined - Stack Overflow

programmeradmin7浏览0评论

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:

puteStats() {
  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 GameTrackerputeStats (/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.

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:

puteStats() {
  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.puteStats (/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.

Share Improve this question asked Nov 11, 2016 at 1:32 nawgszynawgszy 511 silver badge10 bronze badges 4
  • I have no errors here. Can you update your post with full code? – aring 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)}); – Zilvinas Commented Nov 11, 2016 at 1:48
  • another notice - you have 2 different team vars... – Zilvinas Commented Nov 11, 2016 at 1:50
  • what version of node are you using? there's definitely no problem with forEach, see this: runkit./skhavari/40539854 – skav Commented Nov 11, 2016 at 1:55
Add a ment  | 

4 Answers 4

Reset to default 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./news/2013/01/advice-javascript-semicolon-haters/

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 ma operator causes ['away', 'home'] to be the subscript, which gets passed through Object.prototype.toString(), being '[object Array]'.

this.calculateEventData() returns undefined. The statements bees 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._pile (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

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 )

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;

    //...
}
发布评论

评论列表(0)

  1. 暂无评论