我正在尝试使用Sequelize连接到postgres数据库。当我尝试在数据库中创建一行时,我收到以下错误消息。我不确定为什么端口列出3306时我正在尝试连接的数据库URL是端口5432.我尝试在配置文件中设置端口和主机名,但我仍然得到相同的错误。
{
"name": "SequelizeConnectionRefusedError",
"message": "connect ECONNREFUSED 127.0.0.1:3306",
"parent": {
"code": "ECONNREFUSED",
"errno": "ECONNREFUSED",
"syscall": "connect",
"address": "127.0.0.1",
"port": 3306,
"fatal": true
},
"original": {
"code": "ECONNREFUSED",
"errno": "ECONNREFUSED",
"syscall": "connect",
"address": "127.0.0.1",
"port": 3306,
"fatal": true
}
}
我在我的server.js文件中运行我的应用程序:
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const fs = require('fs')
const PORT = process.env.PORT || 3000
// INSTANTIATE EXPRESS APP
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json({
type: function() {
return true
}
}))
// SET VIEW ENGINE
app.use(express.static(path.join(__dirname, 'public')))
require('./src/server/routes')(app)
app.get('*', function(request, response) {
response.sendFile(path.resolve(__dirname, 'public', 'index.html'))
})
// SET FOLDER TO SERVER STATIC ASSETS
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE');
res.setHeader(
"Access-Control-Allow-Headers",
"Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, username")
//and remove cacheing so we get the most recent comments
res.setHeader('Cache-Control', 'no-cache');
next();
});
// SERVER APP
app.listen(PORT, () => {
console.log('Server running on localhost:' + PORT)
})
我的配置文件设置为使用在线数据库
{
"development": {
"url": "postgres://database@stampy.db.elephantsql.com:5432/znjpptuy",
"dialect": "postgres",
"PORT": 5432,
"HOST": "127.0.0.1"
}
}
答案 0 :(得分:0)
仔细检查后,发现错误与我传递给sequelize的新实例的url有关,在这种情况下,缺少url。
sequelize = new Sequelize(process.env [config.url]);
process.env一直以未定义的形式返回,所以我最终将实际的url传递给了函数。