2

我已经为 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");
  }

但它不断抛出“ TypeError : Cannot read property "url" of underfined ”

我尝试使用和尚作为连接器:

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

这也会引发相同的错误。我可能错误地使用了对象 mongo。

4

4 回答 4

2

看起来 mongo.url 没有定义,尝试像下面这样重构你的代码。

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");
}
于 2014-09-11T18:20:06.150 回答
1

请在我的博客中查看我使用 mongoDB 和 Bluemix 的帖子。在上面的代码中没有定义 mongo。您可以将其向上移动或删除“var”。这应该使它可以被其他代码块访问

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/

问候 Ganesh

于 2014-11-21T05:38:46.627 回答
0

应用程序.js:

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

var mongoUrl;

如果(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;

}

}

}

别的 {

mongoUrl = "localhost:27017/SScheduler";

}

// 数据库

var mongo = require('mongoskin');

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

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

像这样尝试,您不需要明确提及任何凭证细节。

于 2014-09-12T13:10:08.683 回答
0

缺少两件事。

  1. mongourl 没有定义。

2.mongodb版本不用提,可以如下优化代码

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";   

      }
于 2014-11-12T03:26:03.637 回答