权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - Node.js forEach: cannot read property '[object Array]' of undefined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin11浏览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. 暂无评论