i am new to electron so im sorry if my question is childish but i have sent some user inout data as object from renderer to main to store in json file (electron-db) but i want to show all data in a table i tired this by event.reply but it giving my desired result in preload's log only i dont know how to take it from preload to html table here i have attached my code from main
// catch obj This is Main.js
ipcMain.on("message", (event, obj) => {
db.createTable("medicine", (succ, msg) => {
// succ - boolean, tells if the call is successful
if (succ) {
console.log(msg);
} else {
console.log("An error has occured. " + msg);
}
});
console.warn(obj);
if ((db.valid("medicine"), location)) {
db.insertTableContent("medicine", obj, (succ, msg) => {
// succ - boolean, tells if the call is successful
console.log("Success: " + succ);
console.log("Message: " + msg);
});
}
let row;
const app = electron.app || electron.remote.app;
db.getAll("medicine", (succ, data) => {
// succ - boolean, tells if the call is successful
// data - array of objects that represents the rows.
// console.warn(data);
row = data;
});
console.warn(row)
event.reply("message", row)
});
and this is my preload
const {contextBridge, ipcMain, ipcRenderer} = require('electron');
const API = {
sendmsg: (obj) => ipcRenderer.send("message", obj)
}
// const getAllData = function fetchData(){
// ipcRenderer.send("req", "this is requirment for All data")
// }
ipcRenderer.on("message", (_event, row) => {
console.log(row)
})
contextBridge.exposeInMainWorld("api", API);
consol.log(row) giving my an array of all data in my db
but i dont know how to make it availabe in html files script or in renderer
The wiring of your IPC is a bit mixed up and confusing. To simplify your scripts, try separating them into their own functionality. IE: Use your
preload.jsscript to allow communication between the main process and render process(es) only.Not knowing what database you are using, I will assume your database code is correct and functional.
Below is an example of how I would set-up my
preload.jsscript and use it to communicate between processes.This
preload.jsscript uses whitelisted channel names to identify which IPC lines of communication can be used, similar to Node.js eventName.preload.js(main process)For brevity, I have included your database code within the below
main.jsfile.main.js(main process)Lastly, I have tried to implement what the basic functionality of your
index.htmlfile may be like.index.html(render process)