首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Sails连接到

将Sails连接到
EN

Stack Overflow用户
提问于 2018-01-31 19:04:29
回答 1查看 851关注 0票数 0

我正在学习Sails (和NodeJS),并试图设置到的连接。到目前为止我所拥有的是:

一个名为test-project-db的数据库,里面有一个名为TestTable1的表。它有两列:name varchar(20)description varchar(100)

一个用sails new生成的sails项目,以及一个用sails generate api TestTable1生成的模型/组件。

我的文件:-api/model/TestTable1.js

代码语言:javascript
复制
module.exports = {

  attributes: {

    name: {
      type: 'string',
    },

    description: {
      type: 'string',
    },

  }
};
  • config/connections.js:
代码语言:javascript
复制
sqlserver: {
    adapter : 'sails-sqlserver',
    user    : 'sa',
    password: 'passw',
    host    : 'localhost\\SQLEXPRESS',
    database: 'test-project-db'
  }
  • config/models.js s.js:
代码语言:javascript
复制
module.exports.models = {

  connection: 'sqlserver',

  migrate: 'safe',

};

但是,当我使用sails lift运行服务器并转到localhost:1337/testtable1时,我得到了错误:

代码语言:javascript
复制
(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-sqlserver,而是使用sails-mssqlserver,那么(在控制台中):

代码语言:javascript
复制
{"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

在浏览器中:

代码语言:javascript
复制
[Error (E_UNKNOWN) Encountered an unexpected error] Details:  ConnectionError: Failed to connect to localhost:undefined in 60000ms

沿着一个500错误页。

知道我做错什么了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-01 00:14:13

localhost:undefined ->端口未定义,因为您没有在配置中提供它。将port属性添加到config/connections.js中的sqlserver连接中,并将其分配给数据库服务器的端口。

边注:边学边学Node是很难的,特别是因为Sails一开始看起来很吓人。把它一片一片地拿着,你一定会明白的。如果您熟悉MVC框架(如Rails ),那么Sails就会有宾至如归的感觉。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48549631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档