Mongoose没有`connect`方法

时间:2012-02-21 22:14:56

标签: node.js mongodb mongoose

我使用npm在node.js上安装了mongoose。当我尝试连接此代码时:

mongoose = require('mongoose/').Mongoose
@db = mongoose.connect("mongodb://localhost/test")

我收到此错误:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Object function Mongoose() {
  this.connections = [];
  this.plugins = [];
  this.models = {};
  this.modelSchemas = {};
  this.options = {};
  this.createConnection(); // default connection
} has no method 'connect'
    at new ChatService (/home/lolcoder/workspace/sampleApp/src/services/dbService.js:12:26)
    at Object.<anonymous> (/home/lolcoder/workspace/sampleApp/src/app.js:40:17)
    at Object.<anonymous> (/home/lolcoder/workspace/sampleApp/src/app.js:44:4)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)

当终端中的mongo时,我得到:

MongoDB shell version: 2.0.2
connecting to: test

任何人都知道为什么mongoose对象中没有connect()方法?

1 个答案:

答案 0 :(得分:13)

而不是

mongoose = require('mongoose/').Mongoose;

使用

mongoose = require('mongoose/');