我是节点js中的新手,我尝试使用节点js app与mongodb连接,但我无法连接这是我的代码
var mongo = require("mongodb");
var host="127.0.0.1";
//var port="27017";
var port=mongo.Connection.DEFAULT_PORT;
var db=new mongo.Db("nodejs-intoduction",new mongo.Server(host, port,{}));
db.open(function(error){
console.log("We are connected.!" + host + ":" + port);
})
我得到的错误就像......
端口= mongo.Connection.DEFAULT_PORT;
cannote读取未定义的属性'DEFAULT_POST'
答案 0 :(得分:0)
使用此代码
var MongoClient = require('mongodb').MongoClient,
// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
db.close();
});
});