我浏览了几篇 stackoverflow 文章并尝试了不同的连接方式,但它们都不起作用。到目前为止,我已经尝试过:
var mongodb = require('mongodb');
var uri = 'mongodb://Userxxx:Passxxx@ds0URI:PORT/heroku_appXXX';
mongodb.MongoClient.connect(uri, { server: { auto_reconnect: true } }, function (err, db) {
});
崩溃并出现以下错误:
TypeError:无法调用未定义的方法“连接”
然后我尝试了这个:
mongo = require('mongodb')
Server = mongo.Server
Db = mongo.Db
BSON = mongo.BSONPure;
con = null;
server = new Server('xxxxx.mongolab.com', 'PORT', {auto_reconnect: true});
DBCon = new Db('xxxxx', server, {safe: false});
DBCon.open(function(err, db) {
if(!err) {
db.authenticate('xxxxx', 'xxxxx', function(err){
if(!err) con = db;
})
}
});
这给了我一个错误:/app/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:10 number') throw "host and port must be specified [" + host + ":" + port + "]" ; 必须指定主机和端口
有谁知道这样做的正确方法?