My Main server.js code where i start my server and coects to mongoDb Server
if(process.env.MONGOLAB_URI) {
mongoose.coect(process.env.MONGOLAB_URI);
} else {
mongoose.coect("mongodb://localhost/itrack");
}
mongoose.coection.on('open', function() {
// This line is getting printed here
console.log('Mongoose opened.');
});
// When successfully coected
mongoose.coection.on('coected', function () {
console.log('Worker ' + process.pid + ' is alive!');
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port:', app.get('port'));
});
});
// If the coection throws an error
mongoose.coection.on('error',function (err) {
console.log('Mongoose default coection error: ' + err);
process.exit(0);
});
// When the coection is discoected
mongoose.coection.on('discoected', function () {
console.log('Mongoose default coection discoected');
});
Now inside my controller/customer/router.js Controller file,
router.put("/addstud/:route", function(req,res,next) {
var custId = req.user.id;
var studList = req.body;
var route = req.params.route;
mongoose.coection.on('open', function() {
// Never goes inside this
console.log('Mongoose opened.');
});
mongoose.coection.once('open',function(err,co) {
// Nor this
}
});
But inside the same class i can easily retrieved and add data to Db using Mongoose Model.
