javascript - 类型错误 : Object #<MongoClient> has no method 'db'

标签 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.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

注意:我使用的是 Ubuntu 14.04.02,我的 node.js 版本 v0.10.38MongoDB 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 并将变量与应用程序对象相关联并使用应用程序进行初始化,可以这样做:

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”*

关于javascript - 类型错误 : Object #<MongoClient> has no method 'db' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29612474/

相关文章:

javascript - 如何将复选框中的值添加到数组

javascript - Express.js : capture generic routes with numeric Id

node.js - Chatbot 消息不会显示在 Facebook Messenger 聊天头像中

mongodb - 你如何在 NoSQL 中进行原子的、多记录的、相互依赖的操作?

mongodb - 创建现有 mongodb 数据库的子集

javascript - 如何防止仅在 Shopify 网站的文本字段中输入空格

javascript - 选择(几乎)元素旁边的文本

javascript - 为什么我的用于将数据保存到数组中的 Node.js 端点在修改数据时会引发错误?

node.js - 没有获得 req header 授权 token

mongodb 聚合框架 - 获取嵌套数组的第一个文档的字段