从其他配置文件访问配置变量 [英] Accessing config variables from other config files

查看:46
本文介绍了从其他配置文件访问配置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个配置文件中使用另一个配置文件中设置的配置变量时遇到问题.例如

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();
}

这会将所有配置设置转储到控制台 - 我什至可以在那里看到 mongo_db 的配置设置!

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

我很困惑.

推荐答案

您无法访问配置文件中的 sails,因为在处理这些文件时,Sails 配置仍在加载!在 bootstrap.js 中,您可以访问 bootstrap 函数内的配置,因为该函数在加载 Sails 后被调用,但不是上面功能.

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.

无论如何,config/local.js 会合并到所有其他配置文件之上,因此您可以通过这种方式获得所需的内容:

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

如果您确实需要从另一个配置文件访问一个配置文件,您可以始终使用 require,但不建议这样做.由于 Sails 会根据多种因素(包括当前环境)将配置文件合并在一起,因此您可能会阅读一些无效的选项.最好按照预期的方式做事:使用 config/env/* 文件进行环境特定的设置(例如 config/env/production.js),config/local.js 用于特定于单个系统(例如您的计算机)的设置以及用于共享设置的其余文件.

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.

这篇关于从其他配置文件访问配置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆