// connections.js
...
module.exports = {
conn: {
mongodb: {
connect: function() {throw ...},
disconnect: function() {throw ...},
getState: function() {throw...}
},
mysql: {
connect: function() {throw ...},
disconnect: function() {throw ...},
getState: function() {throw ...}
}
},
drivers: {
mongoose: mongoose,
mysql: mysql
},
states: connectionStates,
setup: function(config, cb) {
// provides concrete implementations of connect(), discconnect(),
// getState(), sets up listeners to relay connection events
this.conn.mongodb = setupMongo(config.mongodb);
this.conn.mysql = setupSql(config.mysql);
...
cb();
}
};
现在,如果我将其包括为:
// main.js
var connections = require(__dirname + '/connections'),
conn = connections.conn,
db = conn.mongodb;
// connectionections.setup() not been called yet
exports.foo = function() {
// connections.setup() already been called before this point
db.connect(...); // fails - error thrown - using the abstract function
conn.mongodb.connect(...); // works
}
为什么第一个失败?db
var 应该包含对connections.conn.mongodb
? 至少,我希望两者要么工作,要么不工作。允许第一个失败和第二个成功的区别是什么?谢谢