javascript - 创建模型时 Sequelize 连接错误

标签 javascript node.js sequelize.js

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

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);
});

控制台日志:数据库连接成功。 然而,当我添加这一行时:

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 Node :8.2.1 Sequelize :4.8.3 篮板球:7.3.0 pg-hstore 2.3.2

最佳答案

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

此外,Sequelize cli对于编写大量样板代码确实很有帮助

关于javascript - 创建模型时 Sequelize 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46225611/

相关文章:

javascript - 根据需要表单验证链接字段

javascript - Cypress fixtures 在 TypeScript 中的使用

javascript - Tab 键的 keyup/Keydown 事件不起作用

node.js - 如何在 Sequelize 中使用 LEFT JOIN?

javascript - 如何只写一次条件?

node.js - 为什么无法删除 Node 代理中的传输编码 header ?

node.js - 来自服务器 : * OK The Microsoft Exchange IMAP4 service is ready 的无效问候

node.js - 重新启动服务器时 Express 4 session 不持久

join - 在include in sequelize(node.js express)中使用models.sequelize.col()时出现模棱两可的错误

node.js - Nodejs 使用什么 - Sequelize 在数据库中插入时删除空格