tune2fs
tune2fs

Reputation: 7705

How to define property at runtime in nodejs

I have the following property defined for my database access in nodejs. The problem is that I need also the url parameter defined for a certain function. I have therefore written the helper function getDataUrl()

var config = {
    db: {
        db: 'dbname', // the name of the database
        host: "12.12.12.12", // the ip adress of the database
        port: 10091, // the port of the mongo db
        username: "name", //the username if not needed use undefined
        password: "pw", // the password for the db access
        url:  undefined // also tried url: getDataUrl()
     }

};

function getDataUrl() {
       var dataUrl = "mongodb://";
       if (config.db.username !== undefined) {
           dataUrl += config.db.username + ':' + config.db.password + '@';
       }
       dataUrl += config.db.host + ":" + config.db.port;
       dataUrl += '/' + config.db.db
       return dataUrl;
}

module.exports = config;

However I do not want to call this function but use instead the property config.db.url.

I am at the moment struggling how to do that. I have tried the following:

  1. write url: getDataUrl() this produce: TypeError: Cannot read property 'db' of undefined
  2. call getDataUrl() which then writes the property, however this does not overwrite the url property. When I then read the value the following error occures: Cannot read property 'url' of undefined
  3. write config.db.url = getDataUrl(); this also does not overwrite the url property.

I am very new to JavaScript and nodejs therefore I do not know how to achieve this behavior or if it is even possible.

Upvotes: 0

Views: 884

Answers (2)

Bergi
Bergi

Reputation: 664569

You could try a getter property:

var config = {
    db: {
        db: 'dbname', // the name of the database
        host: "12.12.12.12", // the ip adress of the database
        port: 10091, // the port of the mongo db
        username: "name", //the username if not needed use undefined
        password: "pw", // the password for the db access
        get url() {
            var dataUrl = "mongodb://";
            if (this.username)
                dataUrl += this.username + ':' + this.password + '@';
            dataUrl += this.host + ":" + this.port + '/' + this.db;
            return dataUrl;
        }
    }
};
console.log(config.db.url); // automatically computed on [every!] access

Upvotes: 1

Martin Lantzsch
Martin Lantzsch

Reputation: 1901

To fix

write url: getDataUrl() this produce: TypeError: Cannot read property 'db' of undefined

you should change the "configs" variable to "config" in your getDataUrl() function:

function getDataUrl() {
       var dataUrl = "mongodb://";
       if (config.db.username !== undefined) {
           dataUrl += config.db.username + ':' + config.db.password + '@';
       }
       dataUrl += config.db.host + ":" + config.db.port;
       dataUrl += '/' + config.db.db
       return dataUrl;
}

Upvotes: 0

Related Questions