Electron reply error: An object could not be cloned

Electron reply error: An object could not be cloned

我正在尝试让主应用程序找到某种设备,我一直在尝试使用 ipc 来完成它,但无法通过异步发送和同步发送使其工作。我怀疑主要是试图回复承诺。

渲染器:

const recognizedDevices = ipcRenderer.sendSync('findDevice');
console.log(recognizedDevices);

主要:

ipcMain.on('findDevice', (event) => findDevice(event));
const findDevice = async (event) => {
  let recognizedDevices = await findConnectedDevices();
  if(recognizedDevices){
  console.log("found");
  console.log(recognizedDevices);
  return event.returnValue = recognizedDevices;
  }
//TODO: If no device found
}

主要结果:

    found
[
  HID {
    _events: [Object: null prototype] { newListener: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    _raw: HID {},
    write: [Function: bound write],
    getFeatureReport: [Function: bound getFeatureReport],
    sendFeatureReport: [Function: bound sendFeatureReport],
    setNonBlocking: [Function: bound setNonBlocking],
    readSync: [Function: bound readSync],
    readTimeout: [Function: bound readTimeout],
    getDeviceInfo: [Function: bound getDeviceInfo],
    _paused: true,
    [Symbol(kCapture)]: false
  }
]

我希望在渲染器中收到相同的日志结果,但不幸的是我回来了Error: An object could not be cloned.

如果我尝试用recognizedDevice.length回复,我确实会在前端收到“1”,看来他们之间的沟通很好。似乎问题出在发送对象上。

通过 IPC 通道在主进程和渲染进程之间交换的数据通过 Structured Clone Algorithm 序列化。函数和符号不可序列化,但您正试图将它们运送过来:

 [
   HID {
˟   _events: [Object: null prototype] { newListener: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    _raw: HID {},
˟   write: [Function: bound write],
˟   getFeatureReport: [Function: bound getFeatureReport],
˟   sendFeatureReport: [Function: bound sendFeatureReport],
˟   setNonBlocking: [Function: bound setNonBlocking],
˟   readSync: [Function: bound readSync],
˟   readTimeout: [Function: bound readTimeout],
˟   getDeviceInfo: [Function: bound getDeviceInfo],
    _paused: true,
˟   [Symbol(kCapture)]: false
   }
 ]