What I’m trying to do here is access the context property (or context.settings, more specifically) from within the ready function in the same object. I’m not sure what the correct syntax is to do this.
here’s the code:
module.exports = {
context: {
settings: require('./settings')
},
listen: function(callback) {
listen(context.settings.http.port);
callback(null);
},
ready: function (err) {
if (err)
{
throw err;
}
console.log("Ready and listening at http://localhost:" + context.settings.http.port);
}
};
just for clarification, I’m referring to the line console.log("Ready and listening at http://localhost:" + context.settings.http.port);
Edit: a bit more context (ha)
I did try this.context.settings.http.port, but I get
TypeError: Cannot read property 'settings' of undefined.
Here’s the contents of settings.js, just to be sure…
module.exports = {
db: {
host: '127.0.0.1',
port: 27017,
name: 'jsblogdemo'
},
http: {
port: 3000
}
};
Thanks!
Another possibility is this:
Then the functions have local access to the
contextobject without any worries about how they’re called. And thecontextobject can be kept entirely private if you like.