如何将我的mongodb与Bluemix上的NODEjs应用程序连接?

时间:2014-09-11 07:31:10

标签: node.js mongodb ibm-cloud monk nosql

我为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:无法读取属性" url" of undefined"

我尝试过使用monk作为连接器:

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

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

4 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分: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/

此致 内甚

答案 2 :(得分:0)

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(vcapServices中的var svcName){

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

尝试这样做,您无需明确提及任何凭据详细信息。

答案 3 :(得分:0)

缺少两件事。

  1. mongourl未定义。
  2. 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";   
    
          }