我有一个运行在本地计算机上的MSSQL服务器(全新安装),该服务器包含三个数据库:全局数据库,client0和client1。我想通过在node.js后端上使用Sequelize连接到数据库。
给出以下代码,当我仅尝试连接到一个数据库时,连接建立良好。一旦我尝试连接到多个数据库,就会因拒绝的诺言而引发异常,说(尽管已建立与第一个提供的数据库的连接):
var BeforeEnd = books.timeTo.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
var BeforeBegin = books.timeFrom.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
if (books.timeFrom <= Now && books.timeTo > Now)
{
ToRed();
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeEnd;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
else if (books.timeFrom > Now)
{
DispatcherTimer ColorTimer2 = new DispatcherTimer();
ColorTimer2.Interval = BeforeBegin;
ColorTimer2.Tick += (sender, args) =>
{
ToYellow();
ColorTimer2.Stop();
};
ColorTimer2.Start();
}
else if (books.timeTo == Now)
{
ToGreen();
}
config.js
{ SequelizeConnectionError
at Connection.connection.on (...\server\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:75:16)
at Connection.emit (events.js:182:13)
at Connection.cleanupConnection (...\server\node_modules\tedious\lib\connection.js:576:16)
at Connection.enter (...\server\node_modules\tedious\lib\connection.js:1945:12)
at Connection.transitionTo (...\server\node_modules\tedious\lib\connection.js:981:26)
at Connection.socketError (...\server\node_modules\tedious\lib\connection.js:1675:14)
at Connection.dispatchEvent (...\server\node_modules\tedious\lib\connection.js:992:38)
at Connection.socketError (...\server\node_modules\tedious\lib\connection.js:1010:12)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'SequelizeConnectionError',
parent: 'Connection was closed by remote server',
original: 'Connection was closed by remote server' }
index.js
module.exports = {
port: 8081,
clients: [
{
db: {
database: 'global',
user: 'SA',
password: 'Super123!',
options: {
host: 'localhost',
port: 1434,
dialect: 'mssql',
operatorsAliases: false
}
},
authentication: {
jwtSecret: 'secret'
}
},
{
db: {
database: 'client0',
user: 'SA',
password: 'Super123!',
options: {
host: 'localhost',
port: 1434,
dialect: 'mssql',
operatorsAliases: false
}
}
},
{
db: {
database: 'client1',
user: 'SA',
password: 'Super123!',
options: {
host: 'localhost',
port: 1434,
dialect: 'mssql',
operatorsAliases: false
}
}
}
]
};
app.js
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const config = require('../config');
const dbConnections = [];
config.clients.forEach((client) => {
const sequelize = new Sequelize(
client.db.database,
client.db.user,
client.db.password,
client.db.options
);
const dbConnection = {};
fs
.readdirSync(__dirname)
.filter(file => file !== 'index.js')
.forEach((file) => {
const model = sequelize.import(path.join(__dirname, file));
dbConnection[model.name] = model;
});
dbConnection.sequelize = sequelize;
dbConnection.Sequelize = Sequelize;
dbConnections.push({ ...dbConnection });
});
module.exports = dbConnections;
我将不胜感激任何建议和/或帮助。