Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

javascript - nodejs v8.11.2 .foreach not a function error

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

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' } } 

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]) => {

});

Or a for...in loop:

for(let key in team.player) {
  if(!team.player.hasOwnProperty(key)) continue;
  const value = team.player[key];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...