将 Sails JS 连接到 SQL 服务器 EXPRESS
Connect Sails JS to SQL Server EXPRESS
我正在学习 Sails JS(和 NodeJS),我正在尝试设置与 SQL Server Express 的连接。我目前拥有的:
一个名为 test-project-db
的数据库,其中包含一个名为 TestTable1
的 table。它有两列:name varchar(20)
和 description varchar(100)
.
使用 sails new
生成的帆项目,以及使用 sails generate api TestTable1
制作的 model/component。
我的文件:
- api/models/TestTable1.js
module.exports = {
attributes: {
name: {
type: 'string',
},
description: {
type: 'string',
},
}
};
- config/connections.js:
sqlserver: {
adapter : 'sails-sqlserver',
user : 'sa',
password: 'passw',
host : 'localhost\SQLEXPRESS',
database: 'test-project-db'
}
- config/models.js:
module.exports.models = {
connection: 'sqlserver',
migrate: 'safe',
};
但是当我 运行 使用 sails lift
的服务器并转到 localhost:1337/testtable1
时,我得到错误:
(node:15756) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ConnectionError: Failed to connect to localhost:undefined in 60000ms
(node:15756) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
而且,如果我使用 sails-mssqlserver
而不是 sails-sqlserver
,我会得到(在控制台中):
{"name":"Sails-MSSqlserver","hostname":"DESKTOP-PMN0K03","pid":15928,"level":30,"msg":"Error in __FIND__: { ConnectionError: Failed to connect to localhost:undefined in 60000ms\n at Connection.<anonymous> (D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\mssql\lib\tedious.js:378:25)\n at Object.onceWrapper (events.js:315:30)\n at emitOne (events.js:116:13)\n at Connection.emit (events.js:211:7)\n at Connection.connectTimeout
(D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\tedious\lib\connection.js:467:12)\n at ontimeout (timers.js:475:11)\n at tryOnTimeout (timers.js:310:5)\n at Timer.listOnTimeout (timers.js:270:5)\n name: 'ConnectionError',\n message: 'Failed to connect to localhost:undefined in 60000ms',\n code: 'ETIMEOUT' }","time":"2018-01-31T19:00:27.325Z","v":0}
error: Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
ConnectionError: Failed to connect to localhost:undefined in 60000ms
at Connection.<anonymous> (D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\mssql\lib\tedious.js:378:25)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.connectTimeout (D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\tedious\lib\connection.js:467:12)
at ontimeout (timers.js:475:11)
at tryOnTimeout (timers.js:310:5)
at Timer.listOnTimeout (timers.js:270:5)
Details: ConnectionError: Failed to connect to localhost:undefined in 60000ms
在浏览器中:
[Error (E_UNKNOWN) Encountered an unexpected error] Details: ConnectionError: Failed to connect to localhost:undefined in 60000ms
沿着 500 错误页面。
知道我做错了什么吗?
localhost:undefined
--> 端口未定义,因为您没有在配置中提供它。将 port
属性添加到 config/connections.js
内的 sqlserver
连接中,并为其分配数据库服务器的端口。
旁注:在学习 Node 的同时学习 Sails 的情况很少见,尤其是因为 Sails 起初看起来很吓人。一点一点地看,你一定会掌握它的窍门的。如果您熟悉 MVC 框架,例如 Rails,那么 Sails 会非常适合。
我正在学习 Sails JS(和 NodeJS),我正在尝试设置与 SQL Server Express 的连接。我目前拥有的:
一个名为 test-project-db
的数据库,其中包含一个名为 TestTable1
的 table。它有两列:name varchar(20)
和 description varchar(100)
.
使用 sails new
生成的帆项目,以及使用 sails generate api TestTable1
制作的 model/component。
我的文件: - api/models/TestTable1.js
module.exports = {
attributes: {
name: {
type: 'string',
},
description: {
type: 'string',
},
}
};
- config/connections.js:
sqlserver: {
adapter : 'sails-sqlserver',
user : 'sa',
password: 'passw',
host : 'localhost\SQLEXPRESS',
database: 'test-project-db'
}
- config/models.js:
module.exports.models = {
connection: 'sqlserver',
migrate: 'safe',
};
但是当我 运行 使用 sails lift
的服务器并转到 localhost:1337/testtable1
时,我得到错误:
(node:15756) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ConnectionError: Failed to connect to localhost:undefined in 60000ms
(node:15756) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
而且,如果我使用 sails-mssqlserver
而不是 sails-sqlserver
,我会得到(在控制台中):
{"name":"Sails-MSSqlserver","hostname":"DESKTOP-PMN0K03","pid":15928,"level":30,"msg":"Error in __FIND__: { ConnectionError: Failed to connect to localhost:undefined in 60000ms\n at Connection.<anonymous> (D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\mssql\lib\tedious.js:378:25)\n at Object.onceWrapper (events.js:315:30)\n at emitOne (events.js:116:13)\n at Connection.emit (events.js:211:7)\n at Connection.connectTimeout
(D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\tedious\lib\connection.js:467:12)\n at ontimeout (timers.js:475:11)\n at tryOnTimeout (timers.js:310:5)\n at Timer.listOnTimeout (timers.js:270:5)\n name: 'ConnectionError',\n message: 'Failed to connect to localhost:undefined in 60000ms',\n code: 'ETIMEOUT' }","time":"2018-01-31T19:00:27.325Z","v":0}
error: Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
ConnectionError: Failed to connect to localhost:undefined in 60000ms
at Connection.<anonymous> (D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\mssql\lib\tedious.js:378:25)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.connectTimeout (D:\Práctica 2\test-project\node_modules\sails-mssqlserver\node_modules\tedious\lib\connection.js:467:12)
at ontimeout (timers.js:475:11)
at tryOnTimeout (timers.js:310:5)
at Timer.listOnTimeout (timers.js:270:5)
Details: ConnectionError: Failed to connect to localhost:undefined in 60000ms
在浏览器中:
[Error (E_UNKNOWN) Encountered an unexpected error] Details: ConnectionError: Failed to connect to localhost:undefined in 60000ms
沿着 500 错误页面。
知道我做错了什么吗?
localhost:undefined
--> 端口未定义,因为您没有在配置中提供它。将 port
属性添加到 config/connections.js
内的 sqlserver
连接中,并为其分配数据库服务器的端口。
旁注:在学习 Node 的同时学习 Sails 的情况很少见,尤其是因为 Sails 起初看起来很吓人。一点一点地看,你一定会掌握它的窍门的。如果您熟悉 MVC 框架,例如 Rails,那么 Sails 会非常适合。