我有一个导出配置对象的模块:
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/