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.
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");
}