【问题标题】:Trouble with Electron showOpenDialog [duplicate]Electron showOpenDialog出现问题[重复]
【发布时间】:2021-04-10 06:21:53
【问题描述】:

我有一个 Electron 应用程序,它实现了 GlobalShortcut,旨在打开文件并读取它。我遇到了dialog.showOpenDialog() 方法的问题:

// in the main process
const {dialog} = require('electron');
const {BrowserWindow} = require('electron');

app.on('ready', () => {
    globalShortcut.register('CommandOrControl+O', () => {
        const window = BrowserWindow.getFocusedWindow();

        const options = {
            title: 'Pick a markdown file',
            filters: [
                { name: 'Markdown files', extensions: ['md'] },
                { name: 'Text files', extensions: ['txt'] }
            ]
        };

        dialog.showOpenDialog(window, options
            ).then(result => {
                if (result.canceled === false)
                {
                    // read the file and send it to the renderer process
                    let file = result.filePaths[0];
                    console.log(result.filePaths);
                    const content = fs.readFile(file).toString(); // this is the offending line
                    window.webContents.send('load', content);
                }
            }).catch (err => {
                console.log(err);
            });
    });
});

在运行应用程序并为其提供 Ctrl+O 命令时,我收到以下错误: TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

这是来自 package.json 文件的 sn-p:

"dependencies": {
    "electron": "^12.0.2",
    "electron-version": "^2.0.1",
    "simplemde": "^1.11.2"
  }

showOpenDialog 方法不是更新为使用承诺而不是回调吗?是什么导致了这个问题?

【问题讨论】:

    标签: javascript promise callback electron


    【解决方案1】:

    根据这篇文章:how to fix this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

    这里使用的正确方法是fs.readFileSync(file).toString();。这样就解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2020-04-07
      • 2022-01-16
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      相关资源
      最近更新 更多