Search code examples
javascriptelectron

ipcmain.handle parameter has metadata(?) instead of value


I am a beginner to electron and my app does nothing more than unpack a zip and start a .bat file (it's an installer) i need the user to give a url to a database but when it returns i get in ipcMain: {"sender":{"_windowOpenHandler":null,"ipc":{"_events":{},"_eventsCount":1,"_invokeHandlers":{}},"_events":{"render-process-gone":[null,null]},"_eventsCount":13},"frameId":1,"processId":4}

main.js: (not entire file just handle)

    ipcMain.handle('run-start', async (database) => {
        console.log(`dbUrl in main.js:`, JSON.stringify(database), `Type:`, typeof database);
    const command = path.join(filePath, 'dist', 'setup.bat') + ' ' + database
    try {
        console.log(command)
        // const result = await new Promise((resolve, reject) => {
        //     exec(command, (error, stdout, stderr) => {
        //         if (error) {
        //             reject(`Error: ${stderr}`);
        //         } else {
        //             resolve(stdout);
        //         }
        //     });
        // });
        return result;
    } catch (error) {
        return error;
    }
})

preload.js (entire file):

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electron', {
    selectFolder: () => ipcRenderer.invoke('select-folder'),
    runInstall: (destDir) => ipcRenderer.invoke('run-install', destDir),
    runStart: (dbUrl) => {ipcRenderer.invoke('run-start', dbUrl); console.log(dbUrl)}
});

index.html (just the handler for the clicked btn):

        document.getElementById('startButton').addEventListener('click', async () => {
        console.log(dbUrl)
        await window.electron.runStart(dbUrl)
        alert('ClassyBooks is gestart!');
    });

everywhere the dbUrl is correct except in main.js


Solution

  • The callback of ipcMain.handle has two parameters.

      1. event
      1. args

    You're trying to use the event object as the arguments.

    If you don't need to access the event object, use this instead:

    ipcMain.handle('run-start', async (_, database) => {
      // YOUR CODE
    });