gpt4 book ai didi

javascript - Electron :X不是函数>>>(使用ipcrenderer/ipcmain进行回调)

转载 作者:行者123 更新时间:2023-12-03 12:26:04 30 4
gpt4 key购买 nike

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

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

window :

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)

最佳答案

您正在渲染器中发送参数

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

并尝试在主要过程中接收
function (event, library, succes, error) {...

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

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

关于javascript - Electron :X不是函数>>>(使用ipcrenderer/ipcmain进行回调),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465538/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com