0

I am building an application on electron.js. I want to set my application into production. I have already use electron-forge to build my app and its working perfectly. The only thing is the MySQL database don't connect to the build. But in development the MySQL db connects properly. This is how may main.js looks like

const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const mysql = require("mysql2/promise");
// const { prependListener } = require("process");
// const fs = require("fs");
const ExcelJS = require("exceljs");
const { shell } = require('electron');  
if (process.env.NODE_ENV === 'development') {
    require('dotenv').config({ path: '.development.env' });
} else if (process.env.NODE_ENV === 'production') {
    require('dotenv').config({ path: '.production.env' });
}
let connection;

function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true,
            preload: path.join(__dirname, "Backend/preload.js"),
        },
    });

    win.loadFile("public/index.html");
    win.maximize();
}
if (require('electron-squirrel-startup')) app.quit();

// app.setAccessibilitySupportEnabled(enabled);
app.whenReady().then(() => {
    createWindow();

    app.on("activate", () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
        app.quit();
    }
});

async function connectToDB() {
    // Corrected function name and async keyword
    try {
        connection = await mysql.createConnection({
        // Assign connection to the global variable
            host: process.env.DB_HOST,
            user: process.env.DB_USER,
            password: process.env.DB_PASSWORD,
            database: process.env.DB_DATABASE,
        });
    } catch (error) {
        // Added error parameter
        console.error("Error connecting to database:", error); // Corrected log message and added error parameter
    }
}

// Call connectToDB function when app is ready
app.whenReady().then(connectToDB);

This are my .env file Development

DB_HOST=localhost
DB_USER=test    
DB_PASSWORD=********
DB_DATABASE=invoice

Production

DB_HOST=localhost
DB_USER=test    
DB_PASSWORD=********
DB_DATABASE=invoice

When I run my application with npm start

I get this error

Error connecting to database: Error: Access denied for user ''@'localhost' (using password: YES)
at Object.createConnection (C:\Users\callo\OneDrive\Desktop\Invoice-Generator-using-Electron.js\node_modules\mysql2\promise.js:253:31)
at connectToDB (C:\Users\callo\OneDrive\Desktop\Invoice-Generator-using-Electron.js\main.js:50:34) {
code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000'
}
2
  • Your user name variable is not used or not set as the user name is blank in the error message.
    – Shadow
    Commented Apr 27 at 9:58
  • Yeah i understood that byt whats the fix to it Commented Apr 27 at 11:33

0