电子环境桥返回未定义
我有这4个项目文件:
main.js
preload.js
renderer.js
index.html
节点:17.4.0 电子:18.2.0
我正在尝试在文件系统上打开文本文件,该文件是由 Renderer.js 的点击事件触发的,然后将文本文件的内容加载到< div>
index.html中的标签。
main.js
const {app, BrowserWindow, ipcMain} = require("electron");
const path = require("path");
const fs = require("fs");
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
mainWindow.loadFile(path.join(__dirname, "index.html"));
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
app.on("ready", () => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
function openFile(){
fs.readFile("logs.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return "Error Loading Log File";
}
console.log(data);
return data;
});
}
ipcMain.handle("channel-load-file", openFile);
PRELOAD.JS
const {contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
loadFile: () => ipcRenderer.invoke("channel-load-file")
});
Renderer.js
const btn = document.querySelector("#btn");
btn.addEventListener("click", e => {
let data = window.electronAPI.loadFile();
document.getElementById("main-content").innerText = data;
});
我肯定可以在console.log(数据)内看到日志文件的内容 在 main.js 中
; 未定义。
我相信我错过了一个关键的步骤:preload.js
或renderer.js
有人看到事件链丢失的位置吗?
(我对我的流程的任何改进都非常开放)
I've got these 4 project files:
main.js
preload.js
renderer.js
index.html
Node: 17.4.0
Electron: 18.2.0
I'm attempting to open a text file on my filesystem, triggered by a click event from renderer.js - then load the text file's contents into a <div>
tag in index.html.
main.js
const {app, BrowserWindow, ipcMain} = require("electron");
const path = require("path");
const fs = require("fs");
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
mainWindow.loadFile(path.join(__dirname, "index.html"));
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
app.on("ready", () => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
function openFile(){
fs.readFile("logs.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return "Error Loading Log File";
}
console.log(data);
return data;
});
}
ipcMain.handle("channel-load-file", openFile);
preload.js
const {contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
loadFile: () => ipcRenderer.invoke("channel-load-file")
});
renderer.js
const btn = document.querySelector("#btn");
btn.addEventListener("click", e => {
let data = window.electronAPI.loadFile();
document.getElementById("main-content").innerText = data;
});
I can definitely see the contents of the Log file inside console.log(data);
in the main.js
But, the <div id="main-content"></div>
gets populated with undefined.
I believe I'm missing some crucial step within either: preload.js
or renderer.js
Anyone see where the chain of events is getting lost?
(I'm very open to any improvements to my flow)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
插入
console.log()
在下面的代码中指示handle
content是在openfile
具有之前执行的有机会返回结果。
main.js
(主进程)console.log()
结果是...要解决此问题,让我们从一个回到承诺,因此我们可以在
handle> handle
中等待等待。由于
handle
正在处理承诺,让我们使用句法糖async
和等待
,以便于实现。main.js
(主过程)最后,让我们修改我们在
index.html
文件中检索data
的方式。PS:让我们还将
.toString()
添加到返回的data
(可以确保)。index.html
(渲染过程)Inserting
console.log()
's in the code below indicates that thehandle
content is executed beforeopenFile
hasa chance to return a result.
main.js
(main process)The
console.log()
results are...To fix this, let's change
fs.readFile
from a callback to a promise, so we canawait
for it in thehandle
.As the
handle
is dealing with a promise, let's use the syntactic sugarasync
andawait
for easier implementation.main.js
(main process)Lastly, let's modify the way we retrieve the
data
in theindex.html
file.PS: Let's also add
.toString()
to the returneddata
(just to be sure).index.html
(render process)