Javascript Node.js forEach:无法读取属性';[对象数组]';未定义的
我以前从未见过这个。这个错误发生在Node.js 6.3.0和6.9.1 LTS上,为了解决这个问题,我更新了Node.js 6.3.0和6.9.1 LTS 我试图根据我掌握的一些数据来建立一场比赛的统计数据,这些数据并不特别重要。重要的是,作为我的游戏类的一部分,以下函数失败:Javascript Node.js forEach:无法读取属性';[对象数组]';未定义的,javascript,arrays,node.js,foreach,Javascript,Arrays,Node.js,Foreach,我以前从未见过这个。这个错误发生在Node.js 6.3.0和6.9.1 LTS上,为了解决这个问题,我更新了Node.js 6.3.0和6.9.1 LTS 我试图根据我掌握的一些数据来建立一场比赛的统计数据,这些数据并不特别重要。重要的是,作为我的游戏类的一部分,以下函数失败: computeStats() { var stats , roster , team, opp, scoreState, oppScoreState , TOI = this.calcula
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);
})
}
抛出的错误是
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)
我不知道这怎么可能。数组是硬编码的,就在那里。有什么想法吗?我可以解决这个问题,但我发现这个特定的结构是生成我想要使用的统计数据的最简单的方法。我想是ASI在搞乱你。在
eventCounter=this.calculateEventData()之后添加分号,然后查看它是如何运行的
更多信息:我想你的问题是,你显示的[['home','away'],['away','home']]
实际上有一个变量;并且该变量未定义(可能输入错误或未声明)此之后缺少的分号。calculateEventData()
导致以下括号表示法作为下标访问,而不是就地数组表示法
守则内容如下:
var eventCounter = (this.calculateEventData()[['home', 'away'], ['away', 'home']]).forEach((teams) => { ... });
注意我添加的括号。使['away','home']
成为下标,该下标通过Object.prototype.toString()
,成为'[Object Array]
this.calculateEventData()
返回未定义的。语句变为未定义['[object Array]]
基本上,使用分号,并且可能避免内联数组(或者在它们前面加分号,因为这样比较安全)
使用Node.js运行。也会失败
$node-v
v6.5.0
$node test.js
/home/foo/test.js:4
[[]].forEach(()=>{});
^
TypeError:无法读取未定义的属性“[object Array]”
反对。(/home/foo/test.js:4:1)
在模块处编译(Module.js:556:32)
在Object.Module._extensions..js(Module.js:565:10)
在Module.load(Module.js:473:32)
在TryModule加载时(module.js:432:12)
在Function.Module.\u加载(Module.js:424:3)
位于Module.runMain(Module.js:590:10)
运行时(bootstrap_node.js:394:7)
启动时(bootstrap_node.js:149:9)
在bootstrap_node.js:509:3
我认为@Oka已经解决了您看到的错误
但是,似乎还有另一个问题,即使用字符串数组而不是字符串值对对象进行索引:
(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;
//...
}
我这里没有错误。你能用完整的代码更新你的帖子吗?这真的是你分享的完整代码吗?这在chrome上可以正常工作…:['home','away'],['away','home'].forEach((teams)=>{console.log(teams)});另一个注意事项-您有两个不同的团队变量…您使用的是哪个版本的节点?forEach绝对没有问题,看看这个:谢谢你的大喊,但我相信你有点弄错了<代码>团队
是一个字符串[]
-团队
是一个字符串
<代码>['foo','bar',['bar','foo']].forEach((数组)=>console.log(数组的类型[0])
-->(2)string正如Oka所说,我在二维数组上迭代,所以团队
在每一点上都是字符串
,因此团队[I]
是字符串
。现在我按照建议加了一个分号就行了!很漂亮,谢谢。我想这是我编写变量的风格中的一个缺陷,将来必须小心处理;(function(){..})(
或;[1,2,3].forEach..
这是一个很好的答案,我先接受了另一个答案,但可能是我草率地选择了。谢谢你的帮助和清楚的解释。
// test.js
const func = () => {};
const result = func()
[[]].forEach(() => {});
$ 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
(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;
//...
}