节点多次连接到Mongo

时间:2015-09-02 12:36:58

标签: node.js mongodb mean-stack

按照本教程,尝试设置MEAN服务器: https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/

使用这个git: https://github.com/Hitman666/MEAN_MVC_3rdTutorial

我第一次连接数据库时,工作正常。在CTRL + C然后再次运行“节点服务器”之后,我收到此错误:

c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ connection \ base.js:246 throw message; ^ TypeError:无法在c:\ mean2 \ node_modules \ mongoose \ node_modules的processResults(c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ db.js:1581:31)中读取未定义的属性'length' \ mongodb \ lib \ mongodb \ db.js:1619:20 at c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ db.js:1157:7 at c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ db.js:1890:9 at at Server.Base._callHandler(c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ connection \ base.js:448:41)at c :\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ connection \ server.js:481:18在MongoReply.parseBody(c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ responses \ mongo_reply .js:68:5)at null。 (c:\ mean2 \ node_modules \ mongoose \ node_modules \ mongodb \ lib \ mongodb \ connection \ server.js:439:20)在emit(events.js:107:17)处于null。 (C:\ mean2 \ node_modules \猫鼬\ node_modules \ mongodb的\ lib中\ mongodb的\连接\ connection_pool.js:201:13)

相关的代码块:

development.js:

var port = 1337;

module.exports = {
    port: port,
    db: 'mongodb://localhost/todos'
};

mongoose.js:

var config = require('./config'),
mongoose = require('mongoose');

module.exports = function() {
    var db = mongoose.connect(config.db);
    return db;
};

config.js:

module.exports = require('./env/' + process.env.NODE_ENV + '.js');

server.js:

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var config = require('./config/config'),
    mongoose = require('./config/mongoose'),
    express = require('./config/express'),

var db = mongoose(),
    app = express();

app.listen(config.port);

module.exports = app;
console.log(process.env.NODE_ENV  + ' server running at http://localhost:' + config.port);

另外值得注意的是,如果我再次使用db.dropDatabase(),那么“节点服务器”,它可以正常工作。

1 个答案:

答案 0 :(得分:0)

在终止应用之前,我已经使用这个小片段关闭了与MongoDB的连接:

process.on('SIGINT', function() {
    mongoose.close(function(){
        process.exit();
    });
});