#fs.open based on variable (rather than hard-coding)

16 messages · Page 1 of 1 (latest)

true perch
#

I'm following the SO answer here: https://stackoverflow.com/questions/72119191/electron-contextbridge-returns-undefined/72121733#72121733

To be able to use fs.open in my Electron project. I have it working as long as I have a function that is hard-coded to open one specific file every time, but I don't know how to pass a variable to it in order to be able to fs.open whatever I need to in the given situation

This is working with a hard-coded file path in fiddle: https://gist.github.com/SushiKishi/bb5cc09a66f13c7a2e72dd0cac9ae032

This is my non-working attempt to open based on a variable: https://gist.github.com/SushiKishi/17de131debdbd3ee77be49e73b811341

I know WHY the second one throws an error, but don't know how to go about fixing it. In FileOps.js, line 24:

ipcMain.handle('channel-load-file', async (event, message) => {

  return await openFile( file )

//moving on...

it's throwing the "file not defined" error. If instead of "file" there I put:

return await openFile( 'credits.js' )

then openFile takes credits.js as the value for "file" and opens it as it should. But I don't know how to get that line of code to get a definition for a variable; in my index.html file when I use:

('click', () => { window.electronAPI.loadFile('credits.js')

that value doesn't seem to go anywhere because there's nowhere for it to go on the other end, and that's where I think I'm stuck.

https://gist.github.com/SushiKishi/23c6a0f56acc9c3bfaa439f4f394f4c4

noble escarp
#

How familiar you with js?

true perch
#

Not TOO familiar, I've been able to figure out what I needed to with Google for a few things for my Twitch stream, but I do most of that in PHP to swap and update files as needed, so it's not a ton.

#

And I was planning on using PHP and PHPDesktop for what I was going to do, but I know that's fairly outdated so I wanted to pick up something more fitting for the task.

noble escarp
#

Not Too?

#

Do you know what happens when you trying to use undefined variable?

strange dune
#

PHP or JS the rule is the same... you need a file variabe, then why do you name the variable message?

true perch
#

Oh, wait, you know what, I was treating ipcMain like it was a function, so that's not really even defining variables like I thought it was, I was trying to do this:


console.log(file);
//should output "It worked!"

}

doThing ('It worked!');

#

And I can't do that there

noble escarp
#

Its a function

true perch
#

I mean, like, I was treating it like I was creating a new function. I'm not sure I'm explaining what's in my head well but I understand why what I tried doesn't work.

true perch
#

I think I've narrowed down the issue, because in my fileOps.js (required in main.js),

return await openFile('credits.js')

passes credits.js to openFile() as expected, so it opens credits.js. So I need to get a variable to main.js so it can pass it to openFile(), and that's where I'm getting lost. In my index.htm file, clicking the button does:

('click', () => { window.electronAPI.loadFile('credits.js')

but the 'credits.js' string isn't going where I want it to go. I'm not sure it's even going anywhere, calling electronAPI.loadfile invokes ipcRenderer.invoke("channel-load-file") which doesn't take anything else other than the channel to invoke. And it's in the preloader, so that's all done and dusted by the time I could even figure out what I'd want to pass to it the file to load.

noble escarp
#

Why are you trying to read js file?

true perch
#

Only so that Fiddle would include it in the batch of files it stuck onto the Gist

#

I figured if someone wanted to test what I did, it would throw a "there's no credits.txt" error even if it would otherwise work as it should

strange dune