0

Default is always executed, regardless of the script being run.

Here is my code:

import { Pool } from 'pg';
import config from './../config';

const connectionString = () => {
    switch (process.env.NODE_ENV) {
        case 'development': console.log('0'); return config.db_dev.connectionString;
        case 'production': console.log('1'); return config.db_prod.connectionString;
        case 'test': console.log('2'); return config.db_test.connectionString;
        default: console.log('3'); return config.db_dev.connectionString;
    }
}

export default new Pool({
    connectionString: connectionString(),
    ssl: true,
});

File package.json:

"scripts": {
    "start": "nodemon --exec babel-node src/index.js",
    "devbuild": "SET NODE_ENV=development & babel src --out-dir dev-hyperspotters-backend",
    "build": "SET NODE_ENV=production & babel src --out-dir hyperspotters-backend",
    "test": "SET NODE_ENV=test & mocha --timeout 10000 --require @babel/register"
  },

Where did I make a mistake?

MegaRoks
  • 898
  • 2
  • 13
  • 30

1 Answers1

1

it looks like the connectionString is not passed to the function.

    const connectionString = (process.env.NODE_ENV) => {
        switch(process.env.NODE_ENV){
            case 'development':
                return 0
            case 'production':
                return 1
            case 'test':
                return 2
            default:
                return 3
        }
    }

Pool = {
    connectionString: connectionString(process.env.NODE_ENV),
    ssl: true
}
Edwin
  • 294
  • 2
  • 13
  • Gives an error to the function `Invalid left-hand side in arrow function parameters`. Shows it `=> {` – MegaRoks Apr 26 '19 at 03:48
  • Can you share with us what the content of the left hand side is? – Edwin Apr 26 '19 at 04:19
  • I fixed this error; the `developmentMode` variable was adopted in the function, but this did not solve my problem. The `default` branch is still executed. – MegaRoks Apr 26 '19 at 04:21
  • Great. So now the process.env.NODE_ENV is adopted in the function, I assume. Can you verify that it is actual a string holing one of the cases the switch expression is checking on? – Edwin Apr 26 '19 at 05:20