Electron: dialog.showOpenDialog not returning a promise?
Asked Answered
O

3

7

I would like to show an Open Dialog box from within a rendered script.

I am getting conflicting information from different sources, but, as far as I can tell, the documentation at https://electronjs.org/docs/api/dialog suggests I should be able to use:

const dialog = require('electron').remote.dialog;
dialog.showOpenDialog({ title: '…', defaultPath: '…' })
.then(data=>console.log(data));

The error message I get is:

TypeError: dialog.showOpenDialog(...).then is not a function

That suggests that dialog.showOpenDialog() is not returning a promise as per documentation. The sample in the documentation doesn’t work for me either.

I know that I can use dialog.showOpenDialog(options,callback), and have successfully done so, but why can’t I use .then()?

I also note that if I include the optional BrowserWindow parameter it hangs, so the problem may be broader.

Update:

I have accepted @rball’s answer below regarding versions.

It appears that I was still running Electron 5.x, while the current version is 6.x . The documentation doesn’t specifically mention it, but the return result appears to have changed between versions.

Updating to a new major version not intuitive. Here is what I had to do to update:

npm outdated
npm install electron@latest -g --save

Update 2:

For the sake of completeness, here is the code I use to accommodate two different versions of Electron:

if(dialog.showOpenDialog.then)
    dialog.showOpenDialog({
        title: 'Title',
        defaultPath: '…'
    })
    .then(result=> {
        if(result.canceled) return;
        var files=result.filePaths;
        //  process
    });
else
    dialog.showOpenDialog({
        title: 'Title',
        defaultPath: '…'
    },result=> {
        if(result===undefined) return;
        var files=result;
        //  process
    });
Onset answered 8/9, 2019 at 5:31 Comment(2)
not sure, but maybe catching the error on the promise object ie dialog.showOpenDialog().then().catch() itself can help.Calandra
Shoot just got the same issue. Bummer no one answered it.Singlehandedly
S
2

Run npm outdated and check your version. In my version showOpenDialog was returning a string array and not a promise. After updating it, it worked.

Singlehandedly answered 19/10, 2019 at 5:41 Comment(1)
Thanks for this. I have added some information to my question as to how I updated Electron.Onset
C
11

The same happened to me and my workaround was to apply the then method and get the filePaths i.e

dialog.showOpenDialog({
   properties: ['openDirectory']
}).then((data) => {
   console.log(data.filePaths);
});
Chivers answered 17/11, 2019 at 6:54 Comment(1)
Thanks for this great example. I was able to use it to fix my code. Really helped me out of a tough spot with my code. +100 lifePoints :)Rozamond
T
6

I think you are reading the wrong version of the documentation. Before version 6, dialog.showOpenDialog() returns a String[] rather than a Promise<Object>.

Here is the documentation:

Hope my answer can help you

Tetraploid answered 2/10, 2019 at 17:0 Comment(1)
This is useful to know that they changed the return result, but haven’t mentioned it in their documentation.Onset
S
2

Run npm outdated and check your version. In my version showOpenDialog was returning a string array and not a promise. After updating it, it worked.

Singlehandedly answered 19/10, 2019 at 5:41 Comment(1)
Thanks for this. I have added some information to my question as to how I updated Electron.Onset

© 2022 - 2024 — McMap. All rights reserved.