Kanya Ravi Kanya Ravi - 1 month ago 8
Node.js Question

Mongodb not creating database

I cant create my database 'sms-dev' in mongodb after starting the server but in my console it was printing connected to mongo successfully.I kept my db module inside models folder as db.js and export that module in another file(config.js) inside models folder

Here is the code in db.js file

var db = {
// Connects to mongoDB
connect: function(url, options) {
mongoose.connect(url, options);
mongoose.connection.on('open', function(){
console.log("Connected to mongo successfully");
});

mongoose.connection.on('disconnect', function(){
console.log("Mongo disconnected");
});

mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});

process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});

}
}

module.exports = db;


and my config.js file is

exports.database = {
url: 'mongodb://127.0.0.1:27017/sms-dev',
options: {
db: { native_parser: true,safe:true },
server: { poolSize: 10 }
}
}


I connected this db in server.js as

var dbcon = process.env.MONGOLAB_URI || config.database.url;
db.connect(dbcon, config.database.options);

Answer

It seems that my mongodb was not locked properly so i remove the mongodb.lock file and run with -repair option

Comments