我是一名计算机科学专业的学生,正在尝试构建他的第一个 Web 项目,但在 Heroku 上部署我的应用程序时遇到了问题,使用 Sequelize 迁移我的数据库更准确。 这个问题 ("Self signed certificate" error on Heroku running sequelize db:migrate) 正是我的情况,只是该帖子中给出的解决方案没有解决我的问题。我正在使用 Node.js、Express.js、Sequelize、Postgresql 和 Heroku。
总而言之,我的项目构建得很好,并且似乎也可以很好地运行“node server.js”,查看 Heroku 日志。但是,当我尝试运行“heroku run sequelize db:migrate”时,出现以下错误:
(base) ---@Jangui-MacBookAir adonde-ex % heroku run sequelize db:migrate
Running sequelize db:migrate on ⬢ adonde-kr... up, run.1405 (Free)
Loaded configuration file "config/config.js".
Using environment "production".
**ERROR: self signed certificate**
下面是日志,以防这些知识有所帮助(包括一些控制台日志):
2021-07-10T07:06:11.384076+00:00 app[api]: Release v66 created by user ---@gmail.com
2021-07-10T07:06:11.384076+00:00 app[api]: Set PGSSLMODE config vars by user ---@gmail.com
2021-07-10T07:06:13.205003+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-07-10T07:06:13.390201+00:00 heroku[web.1]: Process exited with status 143
2021-07-10T07:06:15.836183+00:00 heroku[web.1]: Starting process with command `node server.js`
2021-07-10T07:06:18.754324+00:00 app[web.1]: env.DATABASE_URL: postgres://---:---@---:5432/---
2021-07-10T07:06:18.755132+00:00 app[web.1]: env.NODE_ENV: production
2021-07-10T07:06:18.755578+00:00 app[web.1]: process.env[config.use_env_variable]: postgres://---:---@---:5432/---
2021-07-10T07:06:18.755668+00:00 app[web.1]: config.use_env_variable: DATABASE_URL
2021-07-10T07:06:18.755747+00:00 app[web.1]: process.env.DATABASE_URL: postgres://---:---@---:5432/---
2021-07-10T07:06:18.758244+00:00 app[web.1]: config: {
2021-07-10T07:06:18.758244+00:00 app[web.1]: use_env_variable: 'DATABASE_URL',
2021-07-10T07:06:18.758245+00:00 app[web.1]: dialect: 'postgres',
2021-07-10T07:06:18.758245+00:00 app[web.1]: dialectOptions: { ssl: { rejectUnautorized: false } }
2021-07-10T07:06:18.758246+00:00 app[web.1]: }
2021-07-10T07:06:18.758331+00:00 app[web.1]: there exists use_env_variable
2021-07-10T12:49:04.675321+00:00 app[web.1]: process.env.PORT: 18267
2021-07-10T07:06:18.913045+00:00 app[web.1]: (node:4) Warning: Accessing non-existent property 'route' of module exports inside circular dependency
2021-07-10T07:06:18.913046+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-07-10T07:06:18.913137+00:00 app[web.1]: (node:4) Warning: Accessing non-existent property 'route' of module exports inside circular dependency
2021-07-10T07:06:18.913430+00:00 app[web.1]: Express listening on port 18267
2021-07-10T07:06:19.012590+00:00 app[web.1]: Executing (default): SELECT 1+1 AS result
2021-07-10T07:06:19.024197+00:00 app[web.1]: Connection has been established successfully.
2021-07-10T07:06:19.024322+00:00 app[web.1]: DB Sync complete.
2021-07-10T07:06:20.328292+00:00 heroku[web.1]: State changed from starting to up
2021-07-10T07:06:28.699592+00:00 app[api]: Starting process with command `sequelize db:migrate` by user ---@gmail.com
2021-07-10T07:06:34.134211+00:00 heroku[run.9662]: State changed from starting to up
2021-07-10T07:06:34.340596+00:00 heroku[run.9662]: Awaiting client
2021-07-10T07:06:34.370927+00:00 heroku[run.9662]: Starting process with command `sequelize db:migrate`
2021-07-10T07:06:40.723220+00:00 heroku[run.9662]: Process exited with status 1
2021-07-10T07:06:40.865602+00:00 heroku[run.9662]: State changed from up to complete
Server.js 是我设置端口的地方,我有一个模型文件夹,我保存所有模型构建、数据库相关代码。在models/index.js中,我初始化了一个Sequelize实例并连接到db。
models/index.js
'use strict';
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};
console.log("process.env[config.use_env_variable]: ", process.env[config.use_env_variable]);
console.log("config.use_env_variable: ", config.use_env_variable)
console.log("process.env.DATABASE_URL: ", process.env.DATABASE_URL)
console.log("config: ", config);
let sequelize;
if (config.use_env_variable) {
console.log("there exists use_env_variable")
sequelize = new Sequelize(process.env[config.use_env_variable], {
dialect: "postgres",
dialectOptions: {
ssl: {
// Ref.: https://github.com/brianc/node-postgres/issues/2009
rejectUnauthorized: false,
},
},
});
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
// let sequelize;
// sequelize = new Sequelize(
// config.database,
// config.username,
// config.password,
// config
// );
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
/config/config.js
const env = process.env;
console.log("env.DATABASE_URL: ", env.DATABASE_URL);
console.log("env.NODE_ENV: ", env.NODE_ENV);
const development = {
"username": env.POSTGRES_USERNAME,
"password": env.POSTGRES_PASSWORD,
"database": env.POSTGRES_DATABASE,
"host": "127.0.0.1",
"dialect": "postgres"
}
const test = {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
}
const production = {
"use_env_variable": "DATABASE_URL",
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"rejectUnautorized": false,
}
}
}
module.exports = {development, test, production}
app.js
const express = require('express');
const bodyParser = require('body-parser');
// const { sequelize } = require('./models')
// db 관련
const db = require('./models');
class App {
constructor () {
this.app = express();
this.dbConnection();
this.setMiddleWare();
this.getRouting();
}
dbConnection() {
db.sequelize.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.then(() => {
console.log('DB Sync complete.');
// we're not syncing; instead we're authenticating and using sequelize migration
// return db.sequelize.sync({force: true});
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
}
getRouting (){
this.app.use(require('./controllers'));
}
setMiddleWare (){
// 미들웨어 셋팅
this.app.use(express.json());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
}
}
module.exports = new App().app;
server.js
const app = require('./app.js');
const port = process.env.PORT || 3000;
console.log("process.env.PORT: ", process.env.PORT)
app.listen( port, function() {
console.log('Express listening on port', port);
});
package.json
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"nodemon": "^2.0.10",
"patch-package": "^6.4.7",
"pg": "^8.6.0",
"pg-hstore": "^2.3.4",
"sequelize": "^6.6.5",
"sequelize-cli": "^6.2.0"
},
"devDependencies": {
"dotenv": "^10.0.0"
},
我已经尝试解决此问题大约 两天,任何输入都将不胜感激。 请帮忙!
答案 0 :(得分:0)
检查您的 config.js 文件。
你有一个错字。将 rejectUnautorized
更正为 rejectUnauthorized
那应该这样做。
最好的问候并保持安全。