I have a config file for my Node.js application which looks like this:
// Development specific configuration
// ==================================
module.exports = {
....
ip: "localhost",
//Rabbit MQ
rabbitmq: {
username: 'foo',
password: 'bar',
ip: 'amqp://'+this.username+':'+this.password+'@rabbitmq',
queues: {
name: 'foo'
}
},
....
};
Here is what the module looks like where I use this config file:
var config = require ('../../config/environment');
this.con = amqp.connect (config.rabbitmq.ip);
I am getting undefined for the username and password:
amqp://undefined:undefined@rabbitmq
I have tried multiple ways such as:
Creating getter functions inside the object like this:
rabbitmq: { username: 'foo', password: 'bar', getUsername: function (){ return this.username; }, getpassword: function (){ return this.password; }, ip: 'amqp://'+this.getUsername()+':'+this.getpassword()+'@rabbitmq',
Created a self value and assign 'this' and reference it:
self: this, ... ip: 'amqp://'+self.rabbitmq.username+':'+self.rabbitmq.password+'@rabbitmq',
..
I cannot get the scope to properly work. Am I missing something basic? How can I make this happen? Please help.
This will work:
var username = 'foo';
var password = 'bar';
module.exports = {
ip: "localhost",
//Rabbit MQ
rabbitmq: {
username: username,
password: password,
ip: 'amqp://'+username+':'+password+'@rabbitmq',
queues: {
name: 'foo'
}
}
};
You can do something like this:
var config = {
ip : "localhost", //Rabbit MQ
rabbitmq : {
username : 'foo',
password : 'bar',
queues : {
name : 'foo'
}
}
};
config
is now declared in module scope so you can reuse its values
config.rabbitmq.ip = 'amqp://' + config.rabbitmq.username + ':' + config.rabbitmq.password + '@rabbitmq';
module.exports = config;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.