Answer the question
In order to leave comments, you need to log in
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$$
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 }
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}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question