我目前正在尝试使用Node和Mongo进行一些自学习.我一直在查看各种博客条目,并且一直在搞乱使用mongo db工作在节点中编写一个简单的博客.
无论如何,我在使用cloudfoundry时遇到了麻烦.我查看了关于身份验证的mongodb-native用户组,并找到了一些节点脚本:
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); }); }); };
我不能为我的生活得到这个工作.
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].(/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/admin.js:124:12) at [object Object].emit (events.js:67:17) at [object Object]. (/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. (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connection.js:108:16) at Socket.emit (events.js:64:17)
任何能够指出我是一个完整的facepalmer的人都会非常感激.
我也遇到过同样的问题.我更努力地找到了解决方案.
这对所有mongodb开发人员都非常有帮助.
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); } }); };
干杯..