javascript - NodeJS 模块 - 填充数组变成空对象。为什么?

标签 javascript arrays node.js object

我有一个导出配置对象的模块:

module.exports = {
    music : {
        catalog : {
            mysql : {
                requiredFields : {
                    foo : [1,2,3],
                    trackQuery : [
                        {
                            table : 'tracks',
                            alias : 't',
                            foo   : [1,2,3],
                            fields : [
                                'id',
                                'name',
                                'duration'
                            ]
                        },
                        {
                            table : 'artists',
                            alias : 'a',
                            fields : [
                                'id',
                                'name'
                            ]
                        }
                    ]
                }
            }
        }
    }
}

fields 数组在运行时变成空对象。

我这样确认:

var conf = require('musicConfig');
console.log ("requiredFields = %j", conf.music.catalog.mysql.requiredFields);

...输出这个:

requiredFields = ["foo":[1,2,3],"trackQuery":[{"table":"tracks","alias":"t","foo":{},"fields":{}},{"table":"artists","alias":"a","fields":{}}]]

如你所见:

conf.music.catalog.mysql.requiredFields.foo                 // [1,2,3]
conf.music.catalog.mysql.requiredFields.trackQuery[0].foo   // {}  <-- WTF
conf.music.catalog.mysql.requiredFields.trackQuery[0].table // "tracks"

知道发生了什么事吗?是的,我可以将 fields 数组移动到更高级别的命名空间,它会起作用——实际上更上一层楼。如果我将它直接放在 requiredFields 下,它将保持填充状态,但这不是一个理想的解决方案。

我已经 confirmed here它不是 ECMAScript 错误,也不是我的对象结构,因为它按预期工作。

我在具有这些依赖项的 Ubuntu 上运行 Node 0.10.3:

"dependencies" : {
    "express"    : "3.1.0",
    "redis"      : "0.8.2",
    "jade"       : "0.28.2",
    "mysql"      : "2.0.0-alpha7",
    "mongodb"    : "*",
    "config"     : "0.4.22"
}

我认为可能是 config module ,但即使我绕过它,问题仍然存在。

更新

以下是输出:console.log(util.inspect(config.music.catalog.mysql.requiredFields.trackQuery[0], { showHidden:真实,深度:空})); ——尽管它并没有多大帮助。我正在研究更有用的标志,愿意接受建议。

{ table: [Getter/Setter],
  alias: [Getter/Setter],
  foo: [Getter/Setter],
  fields: [Getter/Setter],
  [__watchers]:
   { table:
      [ { [Function]
          [length]: 0,
          [name]: '',
          [arguments]: null,
          [caller]: null,
          [prototype]: { [constructor]: [Circular] } },
        [length]: 1 ],
     alias:
      [ { [Function]
          [length]: 0,
          [name]: '',
          [arguments]: null,
          [caller]: null,
          [prototype]: { [constructor]: [Circular] } },
        [length]: 1 ],
     foo:
      [ { [Function]
          [length]: 0,
          [name]: '',
          [arguments]: null,
          [caller]: null,
          [prototype]: { [constructor]: [Circular] } },
        [length]: 1 ],
     fields:
      [ { [Function]
          [length]: 0,
          [name]: '',
          [arguments]: null,
          [caller]: null,
          [prototype]: { [constructor]: [Circular] } },
        [length]: 1 ] },
  [__propertyValues]:
   { table: [ 'tracks', [length]: 1 ],
     alias: [ 't', [length]: 1 ],
     foo: [ { [__watchers]: {}, [__propertyValues]: {} }, [length]: 1 ],
     fields: [ { [__watchers]: {}, [__propertyValues]: {} }, [length]: 1 ] } }

最佳答案

当使用 config 模块读取文件时,我可以重现问题,所以我认为您没有正确绕过它。

此外,当我在 config/lib/config.js 中增加 DEFAULT_CLONE_DEPTH 时(它似乎不能从外部配置,但我不是很熟悉它),它有效:

> console.log(conf.music.catalog.mysql.requiredFields.trackQuery[0].foo)
[ [Getter/Setter], [Getter/Setter], [Getter/Setter] ]

关于javascript - NodeJS 模块 - 填充数组变成空对象。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983002/

相关文章:

javascript - 在全局范围内扩展 jQuery ajax 的成功

javascript - 鼠标滚动时放大和缩小图像

javascript - JS 脚本由 1 个元素调用,未由其他 3 个元素调用

javascript - 如何引用所有 &lt;input&gt; 和 &lt;textarea&gt; 并在它们具有 :focus? 时执行操作

javascript - 从数组中删除已选择的短语

java - java 中的 JSONArray 迭代

arrays - 分割数组并找到最大值 |max (L) -max (R)|

node.js - Express Validator 的值无效

node.js - 模块化编程和 Node

node.js - 如果没有传递查询字符串,Sequelize如何获取所有数据