javascript - 错误 : Error parsing url: undefined when migrating database to heroku

标签 javascript node.js heroku sequelize.js

我正在使用 Sequelize 作为 ORM,并且正在尝试将我的数据库迁移到 Heroku。运行 heroku run sequelize db:migrate

我只是得到

Loaded configuration file "config/config.js".
Using environment "production".
ERROR: Error parsing url: undefined

这是我的配置文件的样子:
  module.exports = {
  "development": {
    "username": "root",
    "password": "password",
    "database": "vueapp",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "use_env_variable": process.env.DATABASE_URL,
    "dialect": "postgres",
    "ssl": true,
    "dialectOptions": {
      "ssl": true
    }
  }
}

和索引文件
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')[env];

const User = require("./user")
const Hour = require('./hours');

const db = {
  User,
  Hour
};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(config.use_env_variable, config);
} else {
  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 = sequelize['import'](path.join(__dirname, file));
    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;

不完全确定是什么问题。通过 heroku 日志,我看到了一个 throw new Error('Dialect needs to be explicitly supplied as of v4.0.0');
但是我在 config.js 文件中添加了方言。配置文件是 .js 文件还是 .json 文件有关系吗?

澄清一下,实际的 Web 应用程序打开了,我可以看到我设置的错误处理程序。所以我在打开路由 URL 时实际上并没有收到应用程序错误

最佳答案

生产: {
use_env_variable: 'DATABASE_URL',
},

将您的生产更改为此并转到 heroku 设置环境变量

关于javascript - 错误 : Error parsing url: undefined when migrating database to heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53143809/

相关文章:

javascript - ES6 Proxy 返回值中的 handler.set

javascript - HTML/CSS 使图像容器与旁边的容器大小相同

javascript - "Uncaught ReferenceError: require is not defined"使用 Angular 2/webpack

javascript - React 更改组件顺序不会触发重新渲染

python - 如何在 heroku cedar 上安装 scikit-learn?

javascript - 隐藏空输入的零值(数字类型)

javascript - 重构两个作用于不同 HTML 标签的相同函数

javascript - Mongoose 对象关系

node.js - 如何用 Mongoose 排除数组类型字段

ruby-on-rails - 延迟部署 Heroku 应用程序更改