【发布时间】: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