我已经为 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。