尝试设置MEAN服务器,遵循本教程:https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/
使用git:https://github.com/Hitman666/MEAN_MVC_3rdTutorial
第一次我连接到数据库,它工作得很好。在CTRL+C之后,然后再次运行"node server",我得到这个错误:
c:mean2node_modulesmongoosenode_modulesmongodblibmongodbconnectionbase.js:246 throw message;^ TypeError:无法读取未定义的属性'length' at processResults (c:mean2node_modulesmongoosenode_modulesmongodblibmongodbdb.js:1581:31) at c:mean2node_modulesmongoosenode_modulesmongodblibmongodbdb.js:1619:20 at c:mean2node_modulesmongoosenode_modulesmongodblibmongodbdb.js: 157:7 at c:mean2node_modulesmongoosenode_modulesmongodblibmongodbdb.js:1890:9 at Server.Base。_callHandler (c:mean2node_modulesmongoosenode_modulesmongodblibmongodbconnectionbase.js:448:41) at c:mean2node_modulesmongoosenode_modulesmongodblibmongodbconnectionserver.js:481:18 at MongoReply。parseBody (c:mean2node_modulesmongoosenode_modulesmongodblibmongodbresponsesmongo_reply.js:68:5) at null。(c:mean2node_modulesmongoosenode_modulesmongodblibmongodbconnectionserver.js:439:20) at emit (events.js:107:17) at null。(c: 非常刻薄 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()然后再执行"node server",它会正常工作。
问题是"npm install mongoose"没有安装正确版本的mongoose。有一次我编辑了这个包。
在终止应用程序之前,我使用了这个小代码片段来关闭与MongoDB的连接:
process.on('SIGINT', function() {
mongoose.close(function(){
process.exit();
});
});