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

时间:2021-07-20 20:16:36

I have tried the below code for VCAP_SERVICES :

我已经尝试了下面的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 "

但它不断抛出“TypeError:不能读取属性”url“未被罚款”

I have tried using monk as a connector giving :

我试过用monk作为连接器:

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

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

这也会抛出相同的错误。我可能用错了mongo对象。

4 个解决方案

#1


2  

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

它看起来像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");
}

#2


1  

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

请看看我在我的博客中使用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/

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

Ganesh的问候

#3


0  

app.js:

app.js:

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

控制台。日志('VCAP服务:' + JSON.stringify(process.env)。VCAP_SERVICES,null,4));

var mongoUrl;

var mongoUrl;

if(process.env.VCAP_SERVICES) {

如果(process.env.VCAP_SERVICES){

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

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

for (var svcName in vcapServices) {

for (vcapServices中的var svcName)

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

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

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

  break;

}

}

}

}

}

else {

其他{

mongoUrl = "localhost:27017/SScheduler";

mongoUrl = " localhost:27017 / SScheduler”;

}

}

// Database

/ /数据库

var mongo = require('mongoskin');

var mongo =要求(“mongoskin”);

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

var db = mongo。db(mongoUrl { native_parser:真});

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

/ / var db = mongo。db(“mongodb:/ / localhost:27017 / nodetest2 ",{ native_parser:真});- - >本地

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

尝试这样做,您不需要显式地提到任何凭据细节。

#4


0  

two things are missing.

有两件事是失踪。

  1. mongourl is not defined.
  2. mongourl没有定义。

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

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

      }

#1


2  

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

它看起来像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");
}

#2


1  

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

请看看我在我的博客中使用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/

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

Ganesh的问候

#3


0  

app.js:

app.js:

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

控制台。日志('VCAP服务:' + JSON.stringify(process.env)。VCAP_SERVICES,null,4));

var mongoUrl;

var mongoUrl;

if(process.env.VCAP_SERVICES) {

如果(process.env.VCAP_SERVICES){

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

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

for (var svcName in vcapServices) {

for (vcapServices中的var svcName)

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

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

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

  break;

}

}

}

}

}

else {

其他{

mongoUrl = "localhost:27017/SScheduler";

mongoUrl = " localhost:27017 / SScheduler”;

}

}

// Database

/ /数据库

var mongo = require('mongoskin');

var mongo =要求(“mongoskin”);

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

var db = mongo。db(mongoUrl { native_parser:真});

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

/ / var db = mongo。db(“mongodb:/ / localhost:27017 / nodetest2 ",{ native_parser:真});- - >本地

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

尝试这样做,您不需要显式地提到任何凭据细节。

#4


0  

two things are missing.

有两件事是失踪。

  1. mongourl is not defined.
  2. mongourl没有定义。

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

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

      }