2
votes

Trying to set up MEAN server, following this tutorial: https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/

Using this git: https://github.com/Hitman666/MEAN_MVC_3rdTutorial

First time I connect to database, it works fine. After CTRL+C and then running "node server" again, I get this error:

c:\mean2\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:246 throw message; ^ TypeError: Cannot read property 'length' of undefined at processResults (c:\mean2\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1581:31) at c:\mean2\node_modules\mongoose\node_modules\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 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 at 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) at emit (events.js:107:17) at null. (c:\mean2\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:201:13)

Related blocks of code:

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);

Also of note, if I db.dropDatabase() then "node server" again, it works fine.

2
Did you set anything weird in your MongoDB? Like maxClients: 1 or something similar? Otherwise: what happens if you killall node and try again?Florian Wendelborn
I haven't set anything up on on my MongoDB. Trying to figure stuff out through tutorials, so I haven't changed any settings. I should also add I'm on Windows 8.1.Chris Stanley
I meant to add more to this response. I did "taskkill /IM node.exe," and it said the process wasn't found.Chris Stanley

2 Answers

2
votes

The problem was that "npm install mongoose" didn't install the proper version of Mongoose. Once I edited the package.json to the most recent version, everything worked fine.

0
votes

I have used this little snippet to close my connections to MongoDB before terminating the app:

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