Souradeepta Biswas
Souradeepta Biswas

Reputation: 21

How can I connect my mongodb with the NODEjs app on Bluemix?

I have tried the below code for VCAP_SERVICES :

if (process.env.VCAP_SERVICES) {
      var env = JSON.parse(process.env.VCAP_SERVICES);
      if (env['mongodb-2.2']) {
        var mongo = env['mongodb-2.2'][0]['credentials'];
      }
    } else {
           var mongo = {
              "username" : "user1",
              "password" : "secret",
              "url" : "mongodb://user1:secret@localhost:27017/test"
     }
}

//With this as the connector
var MongoClient = mongodb.MongoClient;
var db= MongoClient.connect(mongo.url, function(err, db) {
  if(err) {
    console.log("failed to connect to the database");
  } else {
    console.log("connected to database");
  }

but it keeps throwing " TypeError : Cannot read property "url" of underfined "

I have tried using monk as a connector giving :

var monk = require('monk');
 var db = monk(mongo.url);

This also throws the same error. I may be using the object mongo incorrectly.

Upvotes: 2

Views: 1798

Answers (4)

Tinniam V. Ganesh
Tinniam V. Ganesh

Reputation: 2079

Please take a look at my posts using mongoDB and Bluemix in my blog. In the code above mongo is not defined. You could move it up or remove the 'var'. This should make it accessible to other blocks of the code

http://gigadom.wordpress.com/2014/07/27/a-bluemix-recipe-with-mongodb-and-node-js/ http://gigadom.wordpress.com/2014/08/07/spicing-up-a-ibm-bluemix-cloud-app-with-mongodb-and-nodeexpress/

Regards Ganesh

Upvotes: 1

diwesh
diwesh

Reputation: 294

two things are missing.

  1. mongourl is not defined.

2.you don't need to mention mongodb version ,you can optimize the code like below

console.log('VCAP SERVICES: ' + JSON.stringify(process.env.VCAP_SERVICES, null, 4));

var mongoUrl;

if(process.env.VCAP_SERVICES) {

var vcapServices = JSON.parse(process.env.VCAP_SERVICES);

for (var svcName in vcapServices) {

if (svcName.match(/^mongo.*/)) {  --->this part will take care of mongodb version

      mongoUrl = vcapServices[svcName][0].credentials.uri;

      mongoUrl = mongoUrl || vcapServices[svcName][0].credentials.url;

      break;

    }

  }

} else {

       mongoUrl = "localhost:28001/alpha";   

      }

Upvotes: 0

Anand
Anand

Reputation: 645

app.js:

console.log('VCAP SERVICES: ' + JSON.stringify(process.env.VCAP_SERVICES, null, 4));

var mongoUrl;

if(process.env.VCAP_SERVICES) {

var vcapServices = JSON.parse(process.env.VCAP_SERVICES);

for (var svcName in vcapServices) {

if (svcName.match(/^mongo.*/)) {

  mongoUrl = vcapServices[svcName][0].credentials.uri;

  mongoUrl = mongoUrl || vcapServices[svcName][0].credentials.url;

  break;

}

}

}

else {

mongoUrl = "localhost:27017/SScheduler";

}

// Database

var mongo = require('mongoskin');

var db = mongo.db(mongoUrl, {native_parser:true});

//var db = mongo.db("mongodb://localhost:27017/nodetest2", {native_parser:true});-->local

try like this,you don't need to mention any credential detail explicitly.

Upvotes: 0

Jeff Sloyer
Jeff Sloyer

Reputation: 4964

It looks like mongo.url is not defined, try restructing your code like below.

var mongo = {};

if (process.env.VCAP_SERVICES) {
    var env = JSON.parse(process.env.VCAP_SERVICES);
    if (env['mongodb-2.2']) {
        mongo['url'] = env['mongodb-2.2'][0]['credentials']['uri'];
    }
    } else {
           var mongo = {
              "username" : "user1",
              "password" : "secret",
              "url" : "mongodb://user1:secret@localhost:27017/test"
     }
}

//With this as the connector
var MongoClient = mongodb.MongoClient;
var db = MongoClient.connect(mongo.url, function(err, db) {
if(err) {
    console.log("failed to connect to the database");
} else {
    console.log("connected to database");
}

Upvotes: 2

Related Questions