简体   繁体   English

Node.js forEach:无法读取未定义的属性“ [对象数组]”

[英]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. 我已将Node.js 6.3.0和6.9.1 LTS都更新了此错误,以解决此问题。

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: 重要的是以下功能(属于我的Game类的一部分)失败了:

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. 我可以解决它,但是我发现这种特定的结构是生成想要使用的统计信息的最简单方法。

It's the ASI that is messing with you I guess. 我猜这是ASI惹的祸。 Add a semicolon after eventCounter = this.calculateEventData() and see how it runs. eventCounter = this.calculateEventData()之后添加分号,并查看其运行方式。

more info : http://benalman.com/news/2013/01/advice-javascript-semicolon-haters/ 更多信息: http : //benalman.com/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. this.calculateEventData()之后缺少的分号导致以下括号表示法充当下标访问,而不是就地数组表示法。

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]' . 逗号运算符使['away', 'home']为下标,该下标通过Object.prototype.toString()传递,成为'[object Array]'

this.calculateEventData() returns undefined . this.calculateEventData()返回undefined The statements becomes undefined['[object Array]'] . 语句变为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. 与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

I think your problem is that what you showed as [['home', 'away'], ['away', 'home']] there is actually a variable; 我认为您的问题是您显示为[['home', 'away'], ['away', 'home']]内容实际上是一个变量; and that variable is undefined ( possibly mistyped or undeclared ) 并且该变量是未定义的(可能是错误键入或未声明的)

I think @Oka has worked out the error you are seeing. 我认为@Oka解决了您所看到的错误。

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;

    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM