最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read property 'webContents' of undefined at ipcMainImpl. in electron - Stack Over

programmeradmin1浏览0评论

In my electron projects I'm receiving the following TypeError while trying to send and receive data from different windows.

My Imports

const electron = require("electron");
const url = require("url");
const path = require("path");
const { Menu } = require("electron/main");
const { webContents } = require("electron");
const { app, BrowserWindow, ipcMain } = require("electron");

Place of Error

Send data

<script>
  const electron = require("electron");
  const { ipcRenderer } = electron;

  const form = document.querySelector("form");
  form.addEventListener("submit", submitForm);

  function submitForm(e) {
    e.preventDefault();
    const item = document.querySelector("#item").value;
    ipcRenderer.send("item:add", item);
  }
</script>

Receive data

// Catch item:add
ipcMain.on("item:add", function (e, item) {
  console.log(item);
  mainWindow.webContents.send("item:add", item);
  addWindow.close();
});

In my electron projects I'm receiving the following TypeError while trying to send and receive data from different windows.

My Imports

const electron = require("electron");
const url = require("url");
const path = require("path");
const { Menu } = require("electron/main");
const { webContents } = require("electron");
const { app, BrowserWindow, ipcMain } = require("electron");

Place of Error

Send data

<script>
  const electron = require("electron");
  const { ipcRenderer } = electron;

  const form = document.querySelector("form");
  form.addEventListener("submit", submitForm);

  function submitForm(e) {
    e.preventDefault();
    const item = document.querySelector("#item").value;
    ipcRenderer.send("item:add", item);
  }
</script>

Receive data

// Catch item:add
ipcMain.on("item:add", function (e, item) {
  console.log(item);
  mainWindow.webContents.send("item:add", item);
  addWindow.close();
});
Share Improve this question edited Oct 8, 2020 at 7:08 RiBi asked Oct 8, 2020 at 6:12 RiBiRiBi 8831 gold badge10 silver badges27 bronze badges 1
  • well, what is mainWindow set to? that's not shown in the code – pushkin Commented Oct 8, 2020 at 15:32
Add a ment  | 

3 Answers 3

Reset to default 4

I figured out where my error was lying, so I'm answering my own question.

It seems it I was trying to send data that hadn't fully been received, thus I had to wait for my page to load and then send the data as follows.

ipcMain.on("item:add", function (e, item) {
        mainWindow.webContents.on("did-finish-load", () => {
            mainWindow.webContents.send("item:add", item);
        });
});

For me it was a scope error, I had a let mainWindowat the module level but in the createWindow function I had the line async function createWindow() { const mainWindow = new BrowserWindow

In effect I had two mainWindow instances and the first was null and the second was within the scope of the createWindow function.

I deleted the const and the scope was corrected and the problem resolved.

corrected code: async function createWindow() { mainWindow = new BrowserWindow

My noob error :-)

Try something like this:

import { remote } from 'electron' // ES6
const { remote } = require('electron') // ES5


// stuff
const webContents = remote.getCurrentWindow().webContents

// Do something with webContents
webContents.send("item:add", item);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论