I read the quick start from the Mongoose website and I almost copy the code, but I cannot connect the MongoDB using Node.js.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
exports.test = function(req, res) {
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
console.log("h1");
db.once('open', function callback () {
console.log("h");
});
res.render('test');
};
This is my code. The console only prints h1
, not h
. Where am I wrong?