Node.js nodejs-Mysql如何使用不同的数据库?;
我是nodejs的新手,我制作了两个不同的模型,当它们初始化时,我想使用不同的数据库来获取数据,这里是连接代码:Node.js nodejs-Mysql如何使用不同的数据库?;,node.js,Node.js,我是nodejs的新手,我制作了两个不同的模型,当它们初始化时,我想使用不同的数据库来获取数据,这里是连接代码: try { connection = mysql.createConnection({ host: this.config.host, user: this.config.user, password: this.config.password, database: thi
try {
connection = mysql.createConnection({
host: this.config.host,
user: this.config.user,
password: this.config.password,
database: this.config.database
});
} catch (errorMessage) {
logger.error('Error connecting to MySQL database on ' + this.config.database);
}
connection.connect(function(err){
/**
* Connection needs some time to "get ready?"
*/
setTimeout(function() {
if(err){
deferred.reject(err);
} else {
logger.info('Connected to database' + this.config);
this.connection = connection;
deferred.resolve();
}
}, 1000 );
});
初始化函数:
this.init = function(){
var deferred = Q.defer();
this.connect()
.then(fun1)
.then(fun2)
.then(function(){
deferred.resolve();
},function(err){
deferred.reject(err);
});
return deferred.promise;
};
这是单例函数(我认为它没用):
这些代码在两个不同的模型中使用,当一个从另一个用户的旧连接实例(不同的数据库)获取数据时,我如何修复?您解决了这个问题吗?
myclass.getInstance = function(){
if(this.instance === null){
this.instance = new myclass();
this.instance.init().then(function(){
this.connection.end();
}, function(error){
process.exit();
});
}
return this.instance;
}