Monday, 6 May 2019

Sequelize on NodeJS/ExpressJS return an error when running CLI command db:migrate

I'm following this tutorial Using PostgreSQL and Sequelize to persist our data on medium, and right now I'm stuck at the db:migrate. it's returning this error

Sequelize CLI [Node: 12.1.0, CLI: 5.4.0, ORM: 5.8.2]

Loaded configuration file "config.json".
Using environment "development".

ERROR: Error parsing url: undefined

as you can see I'm using NodeJS version 12.1.0 and Sequelize CLI version 5.4.0 and Sequelize version 5.8.2 and all of them were the latest version.

and before running sequelize db:migrate, I'm running this command first SET DATABASE_URL=postgresql://[user[:password]@][netlocation][:port][/dbname] and it does not returns any error.

but it's returning error after I ran db:migrate

I already tried to find the problem, but I can't found the answer yet. Here is my ./Models/Index.js file.

'use strict';

require('dotenv').config();

import { readdirSync } from 'fs';
import { basename as _basename, join } from 'path';
import Sequelize from 'sequelize';
const basename = _basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](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;

export default db;

if you realize I just changed it to ES6 format which change some codes, but before I change it to ES6, it doesn't work either. and for all the rest of the files I following the tutorial.

Here are the files that I think have a connection:

.env

DATABASE_URL=postgres://postgres:admin@localhost:5432/test_app

.sequelizerc

const path = require('path');

module.exports = {
  "config": path.resolve('./config.json'),
  "models-path": path.resolve('./app/Models'),
  "migrations-path": path.resolve('./migrations')
};

config.json

{
  "development": {
    "use_env_variable": "DATABASE_URL"
  },
  "test": {
    "use_env_variable": "DATABASE_URL"
  },
  "production": {
    "use_env_variable": "DATABASE_URL"
  }
}

If there are some files that I haven't included yet please tell me, and please help me to fix find the solution for this problem. Thank you

OS: Windows 10



from Sequelize on NodeJS/ExpressJS return an error when running CLI command db:migrate

1 comment: