#electronAPI not defined in renderer

43 messages · Page 1 of 1 (latest)

dreamy sequoia
#

hallo, ive been working on a project with IPC, but no matter what i do (even just copying the example on the IPC walkthrough on the website) electronAPI isn't defined when i console.log it in the renderer, can someone help? ty. code:

main file: ```js
const { app, BrowserWindow } = require('electron');
const WebSocket = require('ws');
const path = require('path');

const open = () => {
const win = new BrowserWindow({
width: 800,
minWidth: 800,
height: 600,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
autoHideMenuBar: true,
titleBarStyle: 'hidden',
})

win.loadFile(path.join(__dirname, '/frontend/main.html'));

return win

}

app.whenReady().then(async () => {
const mainWindow = open();

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) open()
  })

  const ws = new WebSocket("ws://localhost:9999");

  ws.on('open', () => {console.log('Websocket connected')})

  ws.on('message', (message) => {
    const data = JSON.parse(message);
    console.log(data);

    switch (data.type) {
      case 'structurearray':
        console.log(JSON.stringify(data.payload));
        mainWindow.webContents.send('fileStructure', JSON.stringify(data.payload))
        break;
    }
  })

});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})preload:js
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
onFileStructureSend: (data) => ipcRenderer.on('fileStructure', (data)),
}); renderer:js
window.electronAPI.onFileStructureSend(function(data) {
console.log(data);
});

dusky sigil
#

you can't pass a function to IPC

dreamy sequoia
#

in the preload?

#

wait i think i uploaded the wrong preload

dusky sigil
#

I'm mean as argument, IPC does not accept functions

#

If you want to call main and get a response simply use invoke/handle

dreamy sequoia
#

in this case im just trying to go main to renderer, based on when i get a websocket response so i dont really want to call main cause then that might need polling, but if im understanding wrong please correct me

dusky sigil
#

so you want main to send a message to renderer without renderer calling main, right?

dreamy sequoia
#

yup

dusky sigil
#

okay

dreamy sequoia
#

and then handle that message in renderer and create a few elements but i havent gotten that far yet

dusky sigil
#

what I said still true, you can't pass a function to IPC

dreamy sequoia
#

where am i passing a function?

dusky sigil
#
window.electronAPI.onFileStructureSend(function(data) {
    console.log(data);
});
#

ah my bad

#

it's a listener

dreamy sequoia
#

no worries haha

dusky sigil
#

okay so first param you receive is an event, not your data

dreamy sequoia
#

right, however if i just use data too it still wont fire

#

which probably says nothing but

dusky sigil
#

okay wait, what's your current error?

dreamy sequoia
#

none, but if i console.log electronAPI or window.electronAPI in the renderer console preload has access to it but not the renderer itself

#

heres a better image where you can see the source

dusky sigil
#

but if it was the case your renderer would throw an error

#

that it can't call onFileStructureSend of undefined

dreamy sequoia
#

true, im also noticing whenever i console.log anything it throws the thing and then undefined so

#

that 2nd one probably isnt the renderer on second thought

dusky sigil
#

if you console.log in the dev console it's the renderer

#

not the preload

dreamy sequoia
#

right, cause of context isolated processes right

dusky sigil
#

and the undefined is your console.log(data)

#

make a test and write console.log("data", data) instead

dreamy sequoia
#

doesnt log anything

#

im wondering if the websocket is sending the message before the window is ready?

dusky sigil
#

maybe

dreamy sequoia
#

ah yup

#

thats it

#

god i hate how i always forget the simpliest fixes lol

#

thanks for helping

dusky sigil
#

I don't think I helped much xD

dreamy sequoia
#

i mean

#

at least moral support xD