我是 node.js,mongodb,express 的新手,并且设置数据库时遇到了很多麻烦。我的代码(app.js)在下面给出
var express = require('express'),
app = express(),
cons = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server;
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
var mongoclient = new MongoClient(new Server("localhost", 27017));
// error occurs here
var db = mongoclient.db('course');
app.get('/', function(req, res){
// Find one document in our collection
db.collection('hello_combined').findOne({}, function(err, doc) {
if(err) throw err;
res.render('hello', doc);
});
});
mongoclient.open(function(err, mongoclient) {
if(err) throw err;
app.listen(8080);
});
当我运行此代码节点app.js 时,出现以下错误:
sabbir@sabbir-pc:~/nodejs/hello_express$ node app.js
/home/sabbir/nodejs/hello_express/app.js:12
var db = mongoclient.db('course');
^
TypeError: Object #<MongoClient> has no method 'db'
at Object.<anonymous> (/home/sabbir/nodejs/hello_express /app.js:12:22)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
注意:我使用的是 Ubuntu 14.04.02 ,我的 node.js版本v0.10.38 和 MongoDB shell版本:3.0.2
修改 我还使用以下代码::
var mongoclient = new MongoClient(new Server("localhost", 27017));
mongoclient.open(function(err, mongoclient) {
if(err) throw err;
var db = mongoclient.db('course');
app.get('*', function(req, res){
res.send('Page Not Found', 404);
});
app.get('/', function(req, res){
// Find one document in our collection
db.collection('hello_combined').findOne({}, function(err, doc) {
if(err) throw err;
res.render('hello', doc);
});
});
app.listen(8080);
});
我有以下错误::
sabbir@sabbir-pc:~/nodejs/hello_express$ node app.js
/home/sabbir/nodejs/hello_express/app.js:13
mongoclient.open(function(err, mongoclient) {
^
TypeError: Object #<MongoClient> has no method 'open'
at Object.<anonymous> (/home/sabbir/nodejs/hello_express/app.js:13:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
答案 0 :(得分:5)
如果想要使用MongoClient,允许连接到MongoDB,请执行以下操作:
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
var url = 'mongodb://localhost:27017/test';
Connect using MongoClient
MongoClient.connect(url, function(err, db) {
var testDb = db.db('test'); //use can chose the database here
db.close();
});
另一种方法:
但是,如果你想使用express并希望将变量与app对象关联起来并使用app进行初始化,可以这样做:
var express = require('express'),
app = express(),
cons = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
MongoServer = require('mongodb').Server,
Db = require('mongodb').Db,
db = new Db('pcat', new MongoServer('localhost', 27017, { 'native_parser': true }));
db.open(function (err, asdf) {
app.listen(8080);
console.log("The server is listening at port 8080");
});
* Package.json :
答案 1 :(得分:-1)
执行以下操作:
npm install mongodb@1.4
编辑:
mongoclient.db()不在最新版本的mongodb节点库中。