Electron取消文件打开对话框时出错。

4
当我按下文件打开对话框上的取消按钮(下面是代码),我会收到以下错误:
A JavaScript error occurred in the main process

Uncaught Exception:
TypeError: Error processing argument -1.
    at TypeError (native)
    at [object Object].Object.defineProperty.set (/Applications/Electron.app/Contents/Resources/atom.asar/browser/api/lib/web-contents.js:92:24)
    at main.js:37:20
    at Function.wrappedCallback (/Applications/Electron.app/Contents/Resources/atom.asar/browser/api/lib/dialog.js:80:16)

错误以模态弹出窗口的形式呈现:

Error Popup

我目前只在Mac上进行了测试。
我的代码如下:
处理对话框创建的主要进程代码如下:
import electron, { ipcMain } from 'electron'

const app = electron.app
const BrowserWindow = electron.BrowserWindow

electron.crashReporter.start()

let mainWindow = null

app.on('window-all-closed', () => {
  if (process.platform != 'darwin') app.quit()
})

app.on('ready', () => {

  mainWindow = new BrowserWindow({
    width: 800,
    height: 600
  })

  mainWindow.loadURL(`file://${__dirname}/index.html`)
  mainWindow.webContents.openDevTools()

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  ipcMain.on('file:open', (e, arg) => {
    electron.dialog.showOpenDialog(mainWindow, {
      properties: ['openFile'],
      filters: [{
        name: 'Outlines',
        extensions: ['pxo']
      }]
    }, (filePathList) => {
      debugger
      e.returnValue = filePathList
    })
  })

})

我的调试语句从未被执行。当选择了某些文件时,代码可以正常工作,只有在取消选择时才会失败。
触发IPC调用的代码(ES6 + JSX)如下:
class Welcome extends React.Component {

  render() {
    const styles = this.getStyles()

    return (
      <div style={styles.outerContainer}>
              <a style={styles.btnSetLink} onClick={this.initFileSelection.bind(this)}>
                Open File
              </a>
      </div>
    )
  }

  initFileSelection() {
    const filePathList = ipcRenderer.sendSync('file:open')
  }

  ...

代码通过babel 6进行处理。我的.babelrc如下:
{
  "presets": ["react", "es2015"]
}

电子版本:v0.35.1

Babel依赖的版本:

├─┬ babel-preset-es2015@6.3.13
│ ├── babel-plugin-check-es2015-constants@6.3.13
│ ├── babel-plugin-transform-es2015-arrow-functions@6.3.13
│ ├── babel-plugin-transform-es2015-block-scoped-functions@6.3.13
│ ├─┬ babel-plugin-transform-es2015-block-scoping@6.3.13
│ │ ├─┬ babel-template@6.3.13
│ │ │ └── babylon@6.3.18
│ │ ├─┬ babel-traverse@6.3.19
│ │ │ ├── babylon@6.3.18
│ │ │ ├── globals@8.15.0
│ │ │ └─┬ repeating@1.1.3
│ │ │   └─┬ is-finite@1.0.1
│ │ │     └── number-is-nan@1.0.0
│ │ └─┬ babel-types@6.3.18
│ │   ├── esutils@2.0.2
│ │   └── to-fast-properties@1.0.1
│ ├─┬ babel-plugin-transform-es2015-classes@6.3.15
│ │ ├── babel-helper-define-map@6.3.13
│ │ ├── babel-helper-function-name@6.3.15
│ │ ├── babel-helper-optimise-call-expression@6.3.13
│ │ ├── babel-helper-replace-supers@6.3.13
│ │ └── babel-messages@6.3.18
│ ├── babel-plugin-transform-es2015-computed-properties@6.3.13
│ ├── babel-plugin-transform-es2015-destructuring@6.3.15
│ ├── babel-plugin-transform-es2015-for-of@6.3.13
│ ├── babel-plugin-transform-es2015-function-name@6.3.19
│ ├── babel-plugin-transform-es2015-literals@6.3.13
│ ├─┬ babel-plugin-transform-es2015-modules-commonjs@6.3.16
│ │ └── babel-plugin-transform-strict-mode@6.3.13
│ ├── babel-plugin-transform-es2015-object-super@6.3.13
│ ├─┬ babel-plugin-transform-es2015-parameters@6.3.18
│ │ ├─┬ babel-helper-call-delegate@6.3.13
│ │ │ └── babel-helper-hoist-variables@6.3.13
│ │ └── babel-helper-get-function-arity@6.3.13
│ ├── babel-plugin-transform-es2015-shorthand-properties@6.3.13
│ ├── babel-plugin-transform-es2015-spread@6.3.14
│ ├─┬ babel-plugin-transform-es2015-sticky-regex@6.3.13
│ │ └── babel-helper-regex@6.3.13
│ ├── babel-plugin-transform-es2015-template-literals@6.3.13
│ ├── babel-plugin-transform-es2015-typeof-symbol@6.3.13
│ ├─┬ babel-plugin-transform-es2015-unicode-regex@6.3.13
│ │ └─┬ regexpu@1.3.0
│ │   ├── esprima@2.7.1
│ │   ├─┬ recast@0.10.33
│ │   │ ├── ast-types@0.8.12
│ │   │ └── esprima-fb@15001.1001.0-dev-harmony-fb
│ │   ├── regenerate@1.2.1
│ │   ├── regjsgen@0.2.0
│ │   └─┬ regjsparser@0.1.5
│ │     └── jsesc@0.5.0
│ └─┬ babel-plugin-transform-regenerator@6.3.18
│   ├── babel-core@6.3.17
│   ├── babel-plugin-syntax-async-functions@6.3.13
│   ├── babylon@6.3.18
│   └── private@0.1.6
├─┬ babel-preset-react@6.3.13
│ ├── babel-plugin-syntax-flow@6.3.13
│ ├── babel-plugin-syntax-jsx@6.3.13
│ ├── babel-plugin-transform-flow-strip-types@6.3.15
│ ├── babel-plugin-transform-react-display-name@6.3.13
│ ├─┬ babel-plugin-transform-react-jsx@6.3.13
│ │ └── babel-helper-builder-react-jsx@6.3.13
│ └── babel-plugin-transform-react-jsx-source@6.3.13
├─┬ babel-register@6.3.13
│ ├─┬ babel-core@6.3.17
│ │ ├─┬ babel-code-frame@6.3.13
│ │ │ ├─┬ chalk@1.1.1
│ │ │ │ ├── ansi-styles@2.1.0
│ │ │ │ ├── escape-string-regexp@1.0.3
│ │ │ │ ├─┬ has-ansi@2.0.0
│ │ │ │ │ └── ansi-regex@2.0.0
│ │ │ │ ├── strip-ansi@3.0.0
│ │ │ │ └── supports-color@2.0.0
│ │ │ ├── js-tokens@1.0.1
│ │ │ └─┬ line-numbers@0.2.0
│ │ │   └── left-pad@0.0.3
│ │ ├─┬ babel-generator@6.3.19
│ │ │ ├─┬ detect-indent@3.0.1
│ │ │ │ ├── get-stdin@4.0.1
│ │ │ │ └── minimist@1.2.0
│ │ │ ├── is-integer@1.0.6
│ │ │ └── trim-right@1.0.1
│ │ ├── babel-helpers@6.3.13
│ │ ├── babylon@6.3.18
│ │ ├── convert-source-map@1.1.2
│ │ ├─┬ debug@2.2.0
│ │ │ └── ms@0.7.1
│ │ ├── json5@0.4.0
│ │ ├─┬ minimatch@2.0.10
│ │ │ └─┬ brace-expansion@1.1.2
│ │ │   ├── balanced-match@0.3.0
│ │ │   └── concat-map@0.0.1
│ │ ├── path-is-absolute@1.0.0
│ │ ├── shebang-regex@1.0.0
│ │ ├── slash@1.0.0
│ │ └── source-map@0.5.3
│ ├── babel-runtime@5.8.34
│ ├── core-js@1.2.6
│ ├─┬ home-or-tmp@1.0.0
│ │ ├── os-tmpdir@1.0.1
│ │ └── user-home@1.1.1
│ ├── lodash@3.10.1
│ ├── path-exists@1.0.0
│ └─┬ source-map-support@0.2.10
│   └─┬ source-map@0.1.32
│     └── amdefine@1.0.0

任何帮助解决这个错误都将不胜感激。
3个回答

3

我也遇到了同样的问题,我通过将返回值强制转换为false(否则它是undefined)来解决它:

ipc.on 'request:save:dialog', (event, params) ->
  params||={}
  event.returnValue = dialog.showSaveDialog(params) || false

0

我使用这个来处理它

process.on('uncaughtException', function (exception) {
   // handle or ignore error
});

你好,请在代码中添加一些细节说明,这样答案就有解释并且更容易理解。 - Chait
这段代码将捕获任何错误,在回调函数中您可以处理它。 - j.kherfan
感谢提供信息。请将评论中的信息添加到答案中,以便完整。请查看http://stackoverflow.com/help/how-to-answer获取有关如何编写良好答案的提示。 - Chait

0
对于我的情况,UI和控制台中都没有JavaScript错误。应用程序在取消文件打开对话框后会像正常关闭一样退出。当我使用electron 5版本时,这种情况发生了。升级到版本8后,问题得到解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接