V
V
Vladimir2019-11-19 07:58:21
Node.js
Vladimir, 2019-11-19 07:58:21

Why does mysql swear if I include variables from dotenv?

Good morning. The data is coming, but for some reason I get an error_access_denied_error, I output the data to the object and connect it - everything is ok.
In a previous similar question, there was a problem with a number, +process.env.COMPLEXITY helped.
Now I assumed the possibility of a non-string output, applied String (), sense 0
And it works locally, but I get an error on the hosting, so I started solving it late
Here is my class and
.env packages:

#Project mode
NODE_ENV=production

#App port
PORT=3000

#Mysql

HOST=localhost

USER=root

PASSWORD=

DATABASE=test

SOCKET=

#Bcrypt

COMPLEXITY=10

#Secret for sessions

SECRET=$$999$$

mysql.js:
const { mysql,dotenv,util } = require('./packages')

console.log(process.env.USER)

class Mysql {

  constructor(host,user,password,database,socket) {

    this.connection
    this.host = host
    this.user = user
    this.password = password
    this.database = database
    this.socket = socket

    if(socket) {
      this.connection = mysql.createPool({
           socketPath: this.socket,
           port: 3306,
           user: this.user,
           password: this.password,
           database: this.database
      })
    }

    else {
      this.connection = mysql.createPool({
           host: this.host,
           port: 3306,
           user: this.user,
           password: this.password,
           database: this.database
      });
    }

    return this.connection
  }

}

const options = {
    host: 'localhost',
    user: 'root',
    password: '',
    db: 'test',
    // socket: '/var/run/mysqld/mysqld.sock'
}

const {host,user,password,db,socket} = options;

const pool = new Mysql(host,user,password,db,socket)

pool.getConnection((err, connection) => {
  if (err) {
    console.error(err.code)
  }
  if (connection) connection.release()
})

pool.query = util.promisify(pool.query)

module.exports = { Mysql,pool }

packages.js:
const dotenv = require('dotenv')
dotenv.config({path: __dirname + '/.env'})
var path = require('path')
const express = require('express')
const session = require('express-session')
const util = require('util')
const mysql = require('mysql')
const fileStore = require('session-file-store')(session)
const passport = require('passport')
const localStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
//Complex hash for bcrypt
const saltRounds = +process.env.COMPLEXITY

module.exports = {dotenv, path, express, session, util, mysql, fileStore, passport, localStrategy, bcrypt, saltRounds}

I get this locally:
5dd37665494ca496484669.png
This is hosted:
5dd387fad92ee012560793.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bpGusar, 2019-11-19
@HistoryART

it’s not clear what is swearing here, because the server has successfully started (the last screen),
but if you count the deprecated line as “swearing”, then you just need to google this error, in theory it will not affect the work, but it’s better to fix it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question