Problem using environment variables with Sequelize

Asked

Viewed 18 times

-1

I’m having a problem when I try to connect my database via Node setting environment variables.

The variables I’m trying to set: file . env

DIALECT_KEY='postgres'
HOST_KEY=127.0.0.1
USERMANE_KEY='postgres'
PASSWORD_KEY='admin'
DATABASE_KEY='properties'

The place where I call them: config/database.js file

module.exports = {
    dialect: process.env.DIALECT_KEY,
    host: process.env.HOST_KEY,
    username: process.env.USERMANE_KEY,
    password: process.env.PASSWORD_KEY,
    database: process.env.DATABASE_KEY,
    define: {
        timestamps: true,
        underscored: true,
    },
};

No . sequelizerc configure as follows: file . sequelizerc

const path = require('path');

module.exports = {
    config: path.resolve(__dirname, 'src', 'config', 'database.js'),
    'migrations-path': path.resolve(__dirname, 'src', 'database', 'migrations')
};

And I also call dotenv: server.js file

require('dotenv').config();
const express = require('express');
const routes = require('./routes');
require('./database');
const app = express();
app.use(express.json());
app.use(routes);
app.listen(3333);

console.log(process.env)

And that’s the mistake I’m getting:

PS A:\Node\projects\teste\backend> yarn sequelize db:migrate
yarn run v1.22.10
$ A:\Node\projects\teste\backend\node_modules\.bin\sequelize db:migrate

Sequelize CLI [Node: 14.17.1, CLI: 6.2.0, ORM: 6.6.5]

Loaded configuration file "src\config\database.js".

ERROR: password authentication failed for user "César"

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I checked and the variables are being created correctly, when I click on the link of the directory that the error gives me, it takes me to the config/database.js, first part of code I put here and honestly, I do not think the error.

  • I don’t know if it looks like an application error. It looks more like an authentication error because of password. console.log(process.env.PASSWORD_KEY) displays correctly? Call require('dotenv').config(); inside the archive config/database.js. By error msg, this must be a rejection of Postgres.

  • So @Cmte Cardinal, I put a console.log() for every variable I need to return, like this: 
console.log(process.env.DIALECT_KEY)
console.log(process.env.HOST_KEY)
console.log(process.env.USERNAME_KEY)
console.log(process.env.PASSWORD_KEY)
console.log(process.env.DATABASE_KEY)
 E got this answer from the sitema: 
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting Node src/server.js
postgres,
localhost,
postgres,
admin,
properties, They appear in the exact order I put above. I edited to separate by commas.

  • "Call require('dotenv').config(); inside the archive config/database.js." tested this?

  • Making of the foramen you suggested by placing the require('dotenv'). config() within database.js gave me this error: PS A: Node Projects test backend> Yarn sequelize db:migrate Yarn run v1.22.10 $ A: Node Projects test backend node_modules.bin sequelize db:migrate Sequelize CLI [Node: 14.17.1, CLI: 6.2.0, ORM: 6.6.5] Loaded Configuration file "src config database.js". No Migrations Were executed, database schema was already up to date. Done in 1.38s.

  • I created a new Migration to test and the error persists

  • There is one question that was not considered. See the configuration key USERMANE_KEY='postgres', now see the error message ERROR: password authentication failed for user "César" this discrepancy suggests that the data used in the connection is being fetched from another source.

  • How else can I fix this? I’ve researched many websites, documentation among others and found nothing that can make you solve... I am truly lost

  • It seems that this variable is already included, it appears so and why I wrote with "_KEY", precisely to differentiate one from the other and yet keeps returning me this way.

  • And when I put "require('dotenv'). config()" in the database.js returned me this following error: "PS A: Node Projects test backend> Yarn sequelize db:migrate Yarn run v1.22.10 $ A: Node Projects test backend node_modules.bin sequelize db:migrate Sequelize CLI [Node: 14.17.1, CLI: 6.2.0, ORM: 6.6.5] Loaded Configuration file "src config database.js". No Migrations Were executed, database schema was already up to date. Done in 1.38s"

  • The error says that the schema in the bank was already updated more it happened even I creating another Migration...

  • 1

    I am sorry, but I believe that the information given in the question is not sufficient to obtain a solution. Maybe need to audit the entire project code.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.