编程语言
首页 > 编程语言> > javascript-TypeError:对象#没有方法’db’

javascript-TypeError:对象#没有方法’db’

作者:互联网

我是mongodb的node.js和mongodb的新手,安装数据库时遇到了很多麻烦.我的代码(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

解决方法:

如果要使用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:

>“合并”:“ ^ 0.13.1”,
>“表达”:“ ^ 3.4.4”,
>“ mongodb”:“ ^ 2.0.40”,
>“痛饮”:“ ^ 1.4.2” *

标签:mongodb,node-js,ubuntu-14-04,javascript
来源: https://codeday.me/bug/20191028/1952671.html