1

How can I use sails.config outside the module.exports? I'm trying to pass sails.config variables to another object, something like below;

var foo = new Foo(sails.config.myconf.myVar);

module.exports {
  bar : function(){
    // Use foo here
    foo.blah();
  }
};

(Same question also asked in a comment in this Create config variables in sails.js? See @jreptak comment)

Community
  • 1
  • 1
aug70co
  • 3,965
  • 5
  • 30
  • 44

3 Answers3

1

Each files of Sails config is a module then if you want to use it, you just have to import it.

Here is an example to import Sails connections of sails.config.connections module.
Be careful about the path of the module in the require, it must be relative.

var connections = require('../../config/connections');
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
1

This was not possible in Sails v0.9. However, this is now possible in Sails v0.10 onwards.

Here's the specific issue on github: https://github.com/balderdashy/sails/issues/1672

So now you can do something like this:

//MyService.js
var client = new Client(sails.config.client);
module.exports = {
   myMethod: function(callback){
      client.doSomething();
   }
}

If you're stuck with Sails v0.9, I would recommend that you follow the workaround specified in the github issue:

 //MyService.js
var client;
module.exports = function(){
  client = client || new Client(sails.config.client);
  return {
    myMethod: function(){
      client.doSomething();
    }
  }
}

Which can be used like so:

//SomeController.js
module.exports = {
   list: function(req,res){
      MyService().myMethod();
   }
}
Russell Santos
  • 411
  • 2
  • 8
0

You can't do this, if you want to access sails.config params you have to create a custom hook http://sailsjs.org/documentation/concepts/extending-sails/hooks and do your 'magic' in it

jaumard
  • 8,202
  • 3
  • 40
  • 63