Search code examples
javascriptnode.jsobjectappfog

Appfog, nodejs, Environment variable: service doesn't contain at one point but does at another


I have the following code in my server.js:

throw new Error('The databasenumber was wrong, or you didn\'t bind one.' + process.env.VCAP_SERVICES + " === " + process.env.VCAP_SERVICES["mysql-5.1"]);

The output at this moment is:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: The databasenumber was wrong, or you didn't bind one.{"mysql-5.1":[{"name":"squarific-data","label":"mysql-5.1","plan":"free","tags":["mysql","mysql-5.1","relational","mysql-5.1","mysql"],"credentials":{"name":"d3d440f0f5eef44ceb9c5e27a94666b58","hostname":"eu01-user01.cbxizyg0fwcn.eu-west-1.rds.amazonaws.com","host":"eu01-user01.cbxizyg0fwcn.eu-west-1.rds.amazonaws.com","port":3306,"user":"uzb6UEDnb5huk","username":"uzb6UEDnb5huk","password":"***"}}]} === undefined
at Object.<anonymous> (/mnt/var/vcap.local/dea/apps/squarific-0-230c9a683dff6b045aa2a237ae9bf515/app/server.js:12:9)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)

While this ofcourse should be:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: The databasenumber was wrong, or you didn't bind one.{"mysql-5.1":[{"name":"squarific-data","label":"mysql-5.1","plan":"free","tags":["mysql","mysql-5.1","relational","mysql-5.1","mysql"],"credentials":{"name":"d3d440f0f5eef44ceb9c5e27a94666b58","hostname":"eu01-user01.cbxizyg0fwcn.eu-west-1.rds.amazonaws.com","host":"eu01-user01.cbxizyg0fwcn.eu-west-1.rds.amazonaws.com","port":3306,"user":"uzb6UEDnb5huk","username":"uzb6UEDnb5huk","password":"***"}}]} === [{"name":"squarific-data","label":"mysql-5.1","plan":"free","tags":["mysql","mysql-5.1","relational","mysql-5.1","mysql"],"credentials":{"name":"d3d440f0f5eef44ceb9c5e27a94666b58","hostname":"eu01-user01.cbxizyg0fwcn.eu-west-1.rds.amazonaws.com","host":"eu01-user01.cbxizyg0fwcn.eu-west-1.rds.amazonaws.com","port":3306,"user":"uzb6UEDnb5huk","username":"uzb6UEDnb5huk","password":"***"}}]
at Object.<anonymous> (/mnt/var/vcap.local/dea/apps/squarific-0-230c9a683dff6b045aa2a237ae9bf515/app/server.js:12:9)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)

It shouldn't be undefined as you can clearly see ["mysql-5.1"] is actually an array. I have no idea why it does this strange behavior and I can't find anywhere why it does this.


Solution

  • The service variable gets passed as a JSON string instead of an object by appfog.