#Returning an error from main to renderer using IPC

37 messages · Page 1 of 1 (latest)

stoic hatch
#

Hey guys i'm trying to connect to my server via a module within electron and return the error/success to be displayed in the renderer. however i'm having some trouble achieving that

preload.ts

      connect: () => ipcRenderer.invoke('connect')
    })```

main.ts
```  ipcMain.handle('connect', () => neo_connect())```

connect.ts
```export const neo_connect = async (): Promise<unknown> => {
  try {
    driver = await neo4j.driver(URI, neo4j.auth.basic(USER, PASSWORD))
    const serverInfo = await driver.getServerInfo()
    console.log(serverInfo)
  } catch (err) {
   // console.log(`${err.message}`)
    return 'test'
  }
}```

renderer.svelte
```  const connect = async (): Promise<void> => {
    const response = await window.neo.connect()
    console.log(response)
  }
  connect()```
bold mirage
#

And what's the problem?

stoic hatch
bold mirage
#

you don't return anything in the try

stoic hatch
#

The Auth credentials are wrong so it should go to the catch

humble lotus
#

ipcMain.handle('connect', () => neo_connect())

#

this is wrong

#

also what you get in console.log(response) ?

stoic hatch
humble lotus
#

nothing is?

#

undefined?

stoic hatch
#

it doesn't give anything

humble lotus
#

O_o

stoic hatch
#

console.log('response is'+response) even doing this, it doesn't log the 'response is' part

bold mirage
#

are you sure the handle and/or invoke are even called?

humble lotus
#

show your console.log from devtools

stoic hatch
#
build the electron preload files successfully
publicUtils.cjs:3874

-----

publicUtils.cjs:3874
dev server running for the electron renderer process at:

dep-df561101.js:12851
  ➜  Local:   http://localhost:5173/
dep-df561101.js:12851
  ➜  Network: use --host to expose
dep-df561101.js:12851

start electron app...
stoic hatch
humble lotus
#

from renderer console

bold mirage
#

the dev console

#

you are not looking into the right console...

stoic hatch
#

..............

#

thanks 😅

stoic hatch
humble lotus
#

its correct

stoic hatch
#

sidenote, does electron have a native store?

humble lotus
#

store?

#

you have access to file system

bold mirage
#

It's just that you could write ipcMain.handle('connect', neo_connect) but that would send the event to your function

stoic hatch
#

state management

#

so login, save session kinda thing

humble lotus
#

what is state management?

#

no

#

you should do that manually

stoic hatch
#

ah got it

#

thanks for the clarity guys 😄

humble lotus
#

but i dont think it usefull to auth into offline app