NodeJS模块

编程入门 行业动态 更新时间:2024-10-27 02:24:28
NodeJS模块 - 填充的数组变成空对象。(NodeJS module - populated arrays turning into empty objects. Why?)

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

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下,它将保持填充,但这不是一个理想的解决方案。

我在这里已经确认它不是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" }

我认为它可能是配置模块 ,但即使我绕过它,问题仍然存在。

UPDATE

以下是输出: console.log(util.inspect(config.music.catalog.mysql.requiredFields.trackQuery[0], { showHidden: true, depth: null })); - 虽然它没有多大帮助。 我正在寻找更多有用的旗帜,对建议持开放态度。

{ 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 ] } }

I have a module which exports a config 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' ] } ] } } } } }

The fields arrays are turning into empty objects at runtime.

I'm confirming like this:

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

...which outputs this:

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

As you can see:

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"

Any idea what's going on? Yes, I can move the fields array to a higher level namespace and it will work - one step up actually. If I put it directly under requiredFields it will stay populated, but that's not an ideal solution.

I've confirmed here that it's not an ECMAScript bug and it's not my object structure, because it works as expected.

I'm running Node 0.10.3 on Ubuntu with these dependencies:

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

I thought it might be that config module, but even when I bypass it the issue remains.

UPDATE

Here is the output from: console.log(util.inspect(config.music.catalog.mysql.requiredFields.trackQuery[0], { showHidden: true, depth: null })); -- though it doesn't really help much. I'm looking into more helpful flags, open to suggestions.

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

I can reproduce the problems when the file is read using the config module, so I don't think you're bypassing it correctly.

Also, when I increase DEFAULT_CLONE_DEPTH in config/lib/config.js (it doesn't seem to be configurable from the outside, but I'm not very familiar with it), it works:

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

更多推荐

本文发布于:2023-07-28 18:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307959.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   NodeJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!