【问题标题】:Sequelize Connection Error when creating model创建模型时出现 Sequelize 连接错误
【发布时间】:2018-02-23 19:43:44
【问题描述】:

当我执行这个程序时,一切正常:

const Sequelize = require("sequelize");
const Conn = new Sequelize("data","postgresql","postgresql", {
   'host' : "localhost",
   'dialect' : "postgres",
   'port' : "1337"
})
// piece of code to be added here
Conn.sync().then(function(){
    console.log('DB connection sucessful.');
}, function(err){
    console.log(err);
});

控制台日志:DB connection successful. 但是,当我添加此行时:

const User = Conn.define('user', {
firstName: {
  type: Sequelize.STRING
},
lastName: {
  type: Sequelize.STRING
}
});

我收到此错误消息:

{ SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:1337
    at connection.connect.err (G:\projects\investcrypto\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:95:24)
    at Connection.connectingErrorHandler (G:\projects\investcrypto\node_modules\pg\lib\client.js:123:14)
    at emitOne (events.js:115:13)
    at Connection.emit (events.js:210:7)
    at Socket.<anonymous> (G:\projects\investcrypto\node_modules\pg\lib\connection.js:71:10)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    name: 'SequelizeConnectionRefusedError',
    parent:
     { Error: connect ECONNREFUSED 127.0.0.1:1337
       at Object.exports._errnoException (util.js:1024:11)
       at exports._exceptionWithHostPort (util.js:1047:20)
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
     code: 'ECONNREFUSED',
     errno: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 1337 },
     original:
     { Error: connect ECONNREFUSED 127.0.0.1:1337
       at Object.exports._errnoException (util.js:1024:11)
       at exports._exceptionWithHostPort (util.js:1047:20)
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
     code: 'ECONNREFUSED',
     errno: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 1337 } }

版本: npm:5.3.0 节点:8.2.1 续集:4.8.3 pg: 7.3.0 pg-hstore 2.3.2

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    尝试将端口设为5432。这是 postgres 的默认端口。确保数据库data 是由用户postgresql 创建的,并且密码确实是postgresql。根据数据库的创建方式,您可能只需将用户名和密码设置为 null

    另外,Sequelize cli 对编写大量样板代码很有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2018-12-12
      相关资源
      最近更新 更多