sab*_*bir 3 javascript mongodb node.js 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.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);
});
Run Code Online (Sandbox Code Playgroud)
当我运行此代码节点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
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是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);
});
Run Code Online (Sandbox Code Playgroud)
我有以下错误::
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
Run Code Online (Sandbox Code Playgroud)
小智 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();
});
Run Code Online (Sandbox Code Playgroud)
另一种方法:
但是,如果您想使用express并希望将变量与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");
});
Run Code Online (Sandbox Code Playgroud)
*Package.json:
| 归档时间: |
|
| 查看次数: |
2088 次 |
| 最近记录: |