【问题标题】:Confusion between mongoose.connection() and mongoose.createConnection()mongoose.connection() 和 mongoose.createConnection() 之间的混淆
【发布时间】:2012-10-21 05:00:01
【问题描述】:

我已经研究了 mongoose 三天了,我对这两种方法的使用有点困惑(我知道“mongoose.connection()”将来会被弃用......)

问题是:当我试图转换(从“mongoose.connection()”到“mongoose.createConnection()”)这个例子的action.js文件https://gist.github.com/2785463它似乎对我不起作用...

这是我的代码...

var mongoose = require('mongoose'),
db = mongoose.createConnection('localhost', 'test');

db.on('error', function () {
  console.log('Error! Database connection failed.');
});

db.once('open', function (argument) {
  console.log('Database connection established!');

  mongoose.connection.db.collectionNames(function (error, names) {
    if (error) {
      console.log('Error: '+ error);
    } else {
      console.log(names);
    };
  });
});

还有我的终端输出(在我的 ubuntu 终端上键入“node test.js”..)

Database connection established!

/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:437
    throw err;
          ^
TypeError: Cannot call method 'collectionNames' of undefined
  at NativeConnection.<anonymous> (/home/_user_/Scrivania/test2.js:11:25)
  at NativeConnection.g (events.js:192:14)
  at NativeConnection.EventEmitter.emit (events.js:93:17)
  at open (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:408:10)
  at NativeConnection.Connection.onOpen (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:415:5)
  at Connection._open (/home/_user_/Scrivania/node_modules/mongoose/lib/connection.js:386:10)
  at NativeConnection.doOpen (/home/_user_/Scrivania/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:47:5)
  at Db.open (/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:287:14)
  at Server.connect.connectCallback (/home/_user_/Scrivania/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:235:7)
  at g (events.js:192:14)

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    如果您不调用 mongoose.connect(),则 mongoose.connection 不包含打开的连接。您应该改用 mongo.createConnection() 调用的返回值(您已保存到 db 中)。

    所以最后一段代码应该改为:

    更新

    db.db.collectionNames(function (error, names) {
      if (error) {
        console.log('Error: '+ error);
      } else {
        console.log(names);
      };
    });
    

    我在Connection 上没有看到collectionNames 方法;看起来您必须将属性向下跟踪到本机连接对象才能访问它(参见上面的代码)。

    【讨论】:

    • 好吧,当我尝试使用 console.log(db.collections) 时,它会打印一个空对象:{},但我'确定我的测试数据库有两个集合(我可以通过 mongo shell 使用 show collections 来验证)。顺便说一句,我会继续我的调查谢谢你的帮助,现在! (也许对不起我的英语不好:S)
    • @cl0udw4lk3r 现在我更新了示例,因为我找到了如何从 Mongoose 连接访问本机驱动程序的 Db 对象的 collectionNames 方法。
    • 不错!有用!非常感谢,也许我应该将行 db = mongoose.createConnection(...) 更改为 connection = mongoose.createConnection (...) 避免错误。
    • @JohnnyHK,我使用的是你上面提到的确切代码,但是回调函数永远不会被调用。如果我重新启动 mongod 进程,则会自动调用回调。为什么没有调用 collectionsNames 回调?
    • @rajkamal 最好将其作为一个新问题发布,其中包含您自己的代码,显示您如何建立连接。
    猜你喜欢
    • 2016-09-22
    • 2015-06-01
    • 2020-01-08
    • 2011-09-22
    • 2013-01-03
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多