Maverick
Maverick

Reputation: 3059

NodeJS module - populated arrays turning into empty objects. Why?

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

Upvotes: 3

Views: 227

Answers (1)

robertklep
robertklep

Reputation: 203231

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

Upvotes: 2

Related Questions