Forgive my noobness in advance plz and ty. Im just getting started in Electron and Im trying to setup a basic app to test some functionality. The primary goal of this being that events on the renderer side would trigger some PS scripts on the main process which would then return their data to the front end, pretty simple. I've run into issues with actually getting the data back however. I was able to trigger the scripts just fine but when I try to run them using 'invoke' instead of 'send' I keep getting all sorts of weird errors and I've been in a tailspin trying to figure it out and wrap my head around the whole thing. Not even 100% sure where to start i'm so bent around the axle with this whole thing. the Important parts of what I'm trying to achieve here are as follows:
preload.js:
contextBridge.exposeInMainWorld('electronAPI', {
runPS: (runFile) => ipcRenderer.invoke('run-ps', runFile)
})
the main.js handler:
try {
const output = await runPS(scriptPath);
return output;
} catch (error) {
console.error('Error running PowerShell script:', error);
throw error; // Re-throw the error to be caught by the renderer process
}
});
and the renderer invoker:
btn.addEventListener('click', async () => {
const scriptPath = 'test.ps1';
runScript(scriptPath);
})
async function runScript(scriptPath) {
try {
const output = await window.electronAPI.invoke('run-ps', scriptPath);
console.log('Script output:', output);
} catch (error) {
console.error('Error running script:', error);
}
}
and im getting this error:
Error running script: TypeError: window.electronAPI.invoke is not a function
at runScript (renderer.js:21:49)
in the window console.
Im sure it's just that I'm very new to this and am doing something stupid. Thank for the help!