电子:X不是函数>>>(使用ipcrenderer / ipcmain进行回调)

时间:2019-06-05 17:43:55

标签: javascript node.js function callback electron

问题
我正在使用Promise(s)在加载库时获取回调。每个承诺都会通过ipcRenderer向ipcMain发送一条消息,并包含一个回调函数。

回调函数将按预期方式调用,但显示错误:“成功不是函数”(请看下面的代码)。

窗口:

function loadLibrary() {
    loadLibraryExtension('Database Manager', 'db').then(() => {
        return loadLibraryExtension('Path Extesnion', 'path');
    }).then(() => {
        return loadLibraryExtension('Excel Extension', 'excel4node');
    }).then(() => {
        return loadLibraryExtension('Auto updater', 'autoupdater');
    }).then(() => {
        onLibrariesLoaded();
    });
}

var loadLibraryExtension = (status, library) => {
    return new Promise(function (resolve, reject) {
        $('#current-load').html(status);
        ipcRenderer.send('load',
            library,
            resolve(),
            reject()
        );
    });
}

主要过程:

var db;
var path;
var excel;
var autoUpdater;

var load = {
    'db': () => { db = require('./dbmanager.js'); },
    'path': () => { path = require('path'); },
    'excel4node': () => { excel = require('excel4node'); },
    'autoupdater': () => { 
        autoUpdater = require('electron-updater');
        autoUpdater.autoDownload = false;
        autoUpdater.autoInstallOnAppQuit = false;
    }
}

ipcMain.on('load', function (event, library, succes, error) {
    try {
        load[library]();
    } catch (err) {
        Error(err);
        error(err);
        return;
    }
    succes();
});

我不希望出现任何错误,但是我不断得到

TypeError: succes is not a function
    at EventEmitter.<anonymous> (C:\git\library-system\src\main.js:40:3)
    at EventEmitter.emit (events.js:194:13)
    at WebContents.<anonymous> (C:\git\library-system\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:390:13)
    at WebContents.emit (events.js:194:13)
TypeError: succes is not a function
    at EventEmitter.<anonymous> (C:\git\library-system\src\main.js:40:3)
    at EventEmitter.emit (events.js:194:13)
    at WebContents.<anonymous> (C:\git\library-system\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:390:13)
    at WebContents.emit (events.js:194:13)
TypeError: succes is not a function
    at EventEmitter.<anonymous> (C:\git\library-system\src\main.js:40:3)
    at EventEmitter.emit (events.js:194:13)
    at WebContents.<anonymous> (C:\git\library-system\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:390:13)
    at WebContents.emit (events.js:194:13)
TypeError: succes is not a function
    at EventEmitter.<anonymous> (C:\git\library-system\src\main.js:40:3)
    at EventEmitter.emit (events.js:194:13)
    at WebContents.<anonymous> (C:\git\library-system\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:390:13)
    at WebContents.emit (events.js:194:13)

1 个答案:

答案 0 :(得分:0)

您要在渲染器中发送args

ipcRenderer.send('load', library, resolve(), reject());

并尝试在主流程中接收

function (event, library, succes, error) {...

首先,您发送了resolve(),这不是开始的功能。 Promise.resolve()返回Promise,而不是函数。

第二,Electron IPC的设计不允许发送不可序列化的值。简而言之,您无法发送函数并在其他进程中执行它。您应该从主进程通过webContents.sendevent.sender.send将信号发送回渲染器,然后让渲染器进程解析进程中的Promise。