12

I am having problems using in a config file a config var set in another config file. E.g.

// file - config/local.js
module.exports = {
  mongo_db : {
    username : 'TheUsername',
    password : 'ThePassword',
    database : 'TheDatabase'
  }
}

// file - config/connections.js
module.exports.connections = {
  mongo_db: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    user: sails.config.mongo_db.username,
    password: sails.config.mongo_db.password,
    database: sails.config.mongo_db.database
  },
}

When I 'sails lift', I get the following error:

user: sails.config.mongo_db.username,
      ^
ReferenceError: sails is not defined

I can access the config variables in other places - e.g, this works:

// file - config/bootstrap.js
module.exports.bootstrap = function(cb) {
  console.log('Dumping config: ', sails.config);
  cb();
}

This dumps all the config settings to the console - I can even see the config settings for mongo_db in there!

I so confuse.

sgress454
  • 24,870
  • 4
  • 74
  • 92
onblur
  • 365
  • 1
  • 5
  • 16

1 Answers1

17

You can't access sails inside of config files, since Sails config is still being loaded when those files are processed! In bootstrap.js, you can access the config inside the bootstrap function, since that function gets called after Sails is loaded, but not above the function.

In any case, config/local.js gets merged on top of all the other config files, so you can get what you want this way:

// file - config/local.js
module.exports = {
  connections: {
    mongo_db : {
      username : 'TheUsername',
      password : 'ThePassword',
      database : 'TheDatabase'
    }
  }
}

// file - config/connections.js
module.exports.connections = {
  mongo_db: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017
  },
}

If you really need to access one config file from another you can always use require, but it's not recommended. Since Sails merges config files together based on several factors (including the current environment), it's possible you'd be reading some invalid options. Best to do things the intended way: use config/env/* files for environment-specific settings (e.g. config/env/production.js), config/local.js for settings specific to a single system (like your computer) and the rest of the files for shared settings.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • Cool - that makes sense. I guess I was being thrown off by @mikermcneil comment on this question: http://stackoverflow.com/questions/18267706/ (see very last comment where he seems to confirm you can access config vars inside of other config files) Thanks - I'll try out using combination of config/env and config/local. – onblur Sep 02 '14 at 19:55
  • Yeah, I'm not sure he was really intending to answer the previous comment! – sgress454 Sep 02 '14 at 20:05
  • 1
    worst framework i've ever seen. leads to a need to duplicate code if you intend to do anything noteworthy in config/http, like say accessing redis – r3wt Jun 17 '16 at 20:11