I'm currently trying to mess around with Node and Mongo for a bit of self learning. Ive been looking at various blog entries and have been messing with getting a simple blog written in node with a mongo db working.
Anyway I'm having trouble making it work in cloudfoundry. I've looked on the mongodb-native usergroup about authentication and found some node script:
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
ArticleProvider = function(dbname,host, port,username,password) {
this.db= new Db(dbname, new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(){
this.db.authenticate(username, password, function(err, db) {
callback(err, db);
});
});
};
I can't for the life of me get this working.
this.db.authenticate(username, password, function() {});
^
TypeError: Cannot call method 'authenticate' of undefined
at /home/ben/NodeJS/sandbox/NodeBlog/articleprovider-mongodb.js:10:17
at /home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/db.js:81:14
at /home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connections/server.js:76:11
at /home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/admin.js:16:12
at [object Object].<anonymous> (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/admin.js:124:12)
at [object Object].emit (events.js:67:17)
at [objec开发者_运维百科t Object].<anonymous> (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connections/server.js:97:12)
at [object Object].emit (events.js:64:17)
at Socket.<anonymous> (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connection.js:108:16)
at Socket.emit (events.js:64:17)
Anyone able to point out where I'm being a complete facepalmer would be much appreciated.
I have also faced same kind of problem. Trying harder I have found the solutions.
This may be very helpful to all the mongodb developers.
var ArticleProvider = function(host, port, username, password) {
this.db= new Db('my_db', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(err,data){
if(data){
data.authenticate(username, password,function(err2,data2){
if(data2){
console.log("Database opened");
}
else{
console.log(err2);
}
});
}
else{
console.log(err);
}
});
};
Cheers..
The easiest way connect this is to use a mongourl
(mongodb://localhost:27017/db...) and the connect
function in the node-mongodb-native
drivers.
Cloudfoundry starts a very specific authenticated database and then tells you about connecting to that database within the environment variables, which can make it tougher to debug. Ideally you want it to work on both your local machine and on Cloudfoundry in basically the same way.
Fortunately, I posted an article on MongoDB.org that walks you through a simple Cloudfoundry setup. It has step-by-step instructions from zero to up and running both locally and on Cloudfoundry.
Plus there's a github code sample.
精彩评论