这是我实际的数据库连接模块:
var mongoose = require('mongoose'),
conn = mongoose.createConnection('localhost', 'doto');
conn.on('error', function (err) {
console.log('Error! DB Connection failed.');
});
conn.once('open', function () {
console.log('DB Connection open!');
});
module.exports = conn;
还有一个我用它的地方
exports.list = function (req, res) {
var conn = require('../lib/db_connection');
conn.once('open', function () { // if i dont wrap with this, the page will not be rendered...
conn.db.collectionNames(function (err, names) {
res.render('list_collections', {
title: 'Collections list',
collections_names: names
});
});
});
}
我的问题是:我真的需要每次都使用 conn.once 吗?有什么建议吗?