Javascript 类型错误:对象#<;MongoClient>;没有方法';db';
我在node.js、mongodb、express是新手,在设置数据库时遇到了很多麻烦。下面给出了我的代码(app.js)Javascript 类型错误:对象#<;MongoClient>;没有方法';db';,javascript,node.js,mongodb,ubuntu-14.04,Javascript,Node.js,Mongodb,Ubuntu 14.04,我在node.js、mongodb、express是新手,在设置数据库时遇到了很多麻烦。下面给出了我的代码(app.js) var express = require('express'), app = express(), cons = require('consolidate'), MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server; app
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);
});
当我运行此代码node 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
我有以下错误::
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
sabbir@sabbir-pc:~/nodejs/hello\u express$node app.js
/home/sabbir/nodejs/hello_express/app.js:13
打开(函数(err,mongoclient){
^
TypeError:对象#没有方法“打开”
在对象上。(/home/sabbir/nodejs/hello\u express/app.js:13:13)
在模块处编译(Module.js:456:26)
在Object.Module.\u extensions..js(Module.js:474:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Function.Module.runMain(Module.js:497:10)
启动时(node.js:119:16)
在node.js:935:3
执行以下操作:
npm install mongodb@1.4
编辑:
mongoclient.db()不在最新版本的mongodb节点库中。如果要使用允许连接到mongodb的mongoclient,请按以下方式操作:
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();
});
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");
});
另一种方法:
但是,如果您希望使用express并希望将变量与应用程序对象关联,并使用应用程序进行初始化,可以按以下方式执行:
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();
});
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:
- “合并”:“^0.13.1”
- “快车”:“^3.4.4”
- “mongodb”:“^2.0.40”
- “swig”:“^1.4.2”*
db
方法之前,请先尝试打开连接。请尝试阅读文档,例如。您不使用db
方法访问数据库。当然,新版本上似乎不再提供该功能,我想知道为什么,我的最佳猜测是它太“复杂”要为此创建一个新变量,请查看它在较新版本()上是如何完成的,db在“MongoClient.connect”函数中被引用mongodb://localhost/your_database_name,函数(err,dbConnection){console.log(“创建并连接数据库”);};