gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:25 26 4
gpt4 key购买 nike

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

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.0pg-hstore 2.3.2

最佳答案

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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com