Search code examples
javascriptnode.jsibm-cloud

Bluemix: Node.js example with sqldb service (VCAP_SERVICES credentials)


I am attempting to use the Node.js SDK runtime starter on bluemix with the SQL service and am finding that the code excerpts from the documentation are incompatible with the current version of the Node.js starter code - looks like the app code has been overhauled recently but the documentation on the bluemix site has not.

I am having difficulty in accessing the VCAP_SERVICE credentials using cfenv, which is what the new version of the starter app uses. On the bluemix dashboard I have this:

{
   "sqldb": [
      {
         "name": "SQL Database-nc",
         "label": "sqldb",
         "plan": "sqldb_free",
         "credentials": {
            "port": 50000,
            "db": "SQLDB",
            "username": "user****",
            "host": "75.126.***.***",
            "hostname": "75.126.***.**",
            "jdbcurl": "jdbc:db2://75.126.***.***:50000/SQLDB",
            "uri": "db2://user*****:********@75.126.****:50000/SQLDB",
            "password": "***********"
         }
      }
   ]
}

and I am trying to access the sqldb service credentials as follows:

var appEnv = cfenv.getAppEnv();
var sqlService = appEnv.getService("sqldb");
console.log("user=" + sqlService.credentials.username);

and I have also tried this:

var appEnv = cfenv.getAppEnv();
var sqlService = appEnv.getService("SQLDB");
console.log("user=" + sqlService.credentials.username);

The app is crashing and reporting that sqlService is null. Feels like I am missing something obvious but could use help in figuring out what that is.

Thank you for any assistance, -Andy


Solution

  • Use the "name" property, "SQL Database-nc".

    From the cfenv documentation...

    "The spec parameter should be a regular expression, or a string which is the exact name of the service."