0

Ok, so this is either very weird or I'm not understanding something that is happening. I am trying to load the sequelize library in node.

when trying to connect I'm using the CLI generated index.js file however this line:

if (config.use_env_variable) {
    console.log('i ran');
  var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
      var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
      dialect:config.db.dialect});
}

is giving me this error:

Cannot read property 'use_env_variable' of undefined

as far as I know that line is meant to see if this even returns anything so I don't understand why this is throwing that error?

UPDATE

config is invoked in a line above it, the whole file up to that point is:

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(__filename);
const config    = require(path.join(__dirname,'../config/config.js'));
const db        = {};

console.log(config);


if (config.use_env_variable) {
    console.log('i ran');
  var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
      var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
      dialect:config.db.dialect});
}

UPDATE added console.log of config on working version

  • config is undefined here. – yBrodsky May 04 '18 at 13:51
  • config is definitely defined as when I remove the if statement and just use regular variables like config.db.username it's vaild. There is however no config.use_env_variable. I get why it's undefined, but not why its causing an error. Shouldn't the else clause just kick in? –  May 04 '18 at 16:12
  • I don't understand. In that screenshot you deleted, we can see that config has no property `use_env_variable`. And you could also see that sequelize was being initialized correctly (because of your else). – yBrodsky May 04 '18 at 16:48
  • yep, I figured it out. I deleted the screenshot because I accidentally used the production set of variables and I cant' really have that sitting out on SO –  May 04 '18 at 17:04
  • So if you figured it out, what was the problem? =P – yBrodsky May 04 '18 at 17:08

2 Answers2

1

After 3 days struggling with error, Today I was able to find a solution.

How did i solve?

I used JS trim() function to remove spaces.

for example

  process.env.NODE_ENV === "development"   // return false

  why ?

 "development " === "development"   // return false because of extra space

process.env.NODE_ENV.trim() === "development" //return true

Here is my solution

Niyongabo Eric
  • 1,333
  • 18
  • 21
0

It looks like you do not have config/config.json file or the path is incorrect. In model/index.js, you would have this line

let config = require(`${__dirname}/../../config/config.json`)[env];

or something like that. Make sure this path is the right path

AbhinavD
  • 6,892
  • 5
  • 30
  • 40