问题描述
我正在尝试将开发人员拥有的nodejs应用添加到生产服务器.运行脚本时出现此错误.
I'm trying to add a nodejs app we have on dev to production server. I'm getting this error when I run the script.
TypeError: team.player.forEach is not a function
我知道team.player是合法的.我控制台记录它,并显示了这一点.
I know team.player is legit. I console log it and it shows this.
player:
{ name: 'TEAM',
shortname: 'TEAM',
checkname: 'TEAM',
uni: 'TM',
class: 'FR',
gp: '1',
code: '198',
rush: { att: '0', yds: '465', gain: '465', loss: '0', td: '0', long: '0' },
pass:
{ comp: '0',
att: '0',
int: '0',
yds: '40',
td: '0',
long: '0',
sacks: '0',
sackyds: '0' },
fumbles: { no: '3', lost: '1' } }
我唯一能弄清楚的是,在开发服务器上,我们使用v8.9.4,在生产环境中使用的版本是8.11.2,尽管我认为在这种情况下这并不重要,也没有听说过任何人否则有这个问题.
The only thing I can figure out is that on the dev server we use v8.9.4 and this version on production we use 8.11.2 though I don't think that should matter in this instance and haven't heard of anyone else having this issue.
推荐答案
类似于 player
的对象不是数组.如果要对其进行迭代,则应使用 Object.values
, Object.keys
或 Object.entries
:
Looks like player
is an object not an array. If You want to iterate over it, you should use Object.values
, Object.keys
, or Object.entries
:
Object.values(team.player).forEach(value => {
});
Object.keys(team.player).forEach(key => {
});
Object.entries(team.player).forEach(([key, value]) => {
});
或 对于...在
循环中:
for(let key in team.player) {
if(!team.player.hasOwnProperty(key)) continue;
const value = team.player[key];
}
这篇关于nodejs v8.11.2 .foreach不是函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!