【问题标题】:Electron error on canceling file open dialog取消文件打开对话框时出现电子错误
【发布时间】:2016-03-25 23:16:17
【问题描述】:

当我在文件打开对话框(下面的代码)上按下取消按钮时,我收到以下错误:

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)

错误显示为模态弹出窗口:

到目前为止,我只在 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

任何帮助解决错误将不胜感激。

【问题讨论】:

    标签: javascript dialog electron


    【解决方案1】:

    同样的问题,我通过将返回值强制为false 来解决(否则为undefined):

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

    【讨论】:

      【解决方案2】:

      我用这个来处理它

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

      【讨论】:

      • 您好,请在代码中添加一些细节,以便答案有一些解释,更容易理解。
      • 此代码将捕获任何错误并在回调中处理它
      • 感谢您的信息。请将评论中的信息添加到答案中,使其完整。请查看stackoverflow.com/help/how-to-answer,了解如何写出好的答案。
      【解决方案3】:

      就我而言,我在 UI 和控制台上都没有任何 javascript 错误。该应用程序会像您在取消文件打开对话框后正常关闭它一样退出。当我使用电子版本 5 时发生这种情况。更新到版本 8 解决了这个问题。

      【讨论】:

        猜你喜欢
        • 2011-05-10
        • 1970-01-01
        • 2011-11-10
        • 2013-03-17
        • 2015-04-06
        • 1970-01-01
        • 2017-10-02
        • 1970-01-01
        • 2012-11-14
        相关资源
        最近更新 更多