I am trying to send a success reply from main.js to my frontend customer.js. I want to send the message but do not know how to handle it on the front end. In the frontend, I am using jQuery. The code works so no need to change that just want to handle the event.
This is my code in main.js
const { app, BrowserWindow, ipcMain } = require("electron");
ipcMain.on("createCustomer", async (event, data) => {
const { customerData } = data;
try {
const insertCustomerQuery = `
INSERT INTO customers (company_name, address, phone, gstin, pan, cin)
VALUES (?, ?, ?, ?, ?, ?)
`;
await connection.query(insertCustomerQuery, [
customerData.companyName,
customerData.address,
customerData.phone,
customerData.gstin,
customerData.pan,
customerData.cin,
]);
event.reply(true);
// win.reload();
console.log("Data inserted successfully");
} catch (error) {
event.reply("createcustomerfailed", error);
console.error("Error inserting data:", error);
}
});
In frontend, customer.js the code looks like
$("#saveCustomer").click(async () => {
try {
const customerData = saveCustomerData();
await window.electron.send('createCustomer', { customerData });
console.log(result);
} catch (error) {
console.log(error);
}
});
I want to handle the reply here.
for reference this is my preload.js
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld(
'electron',
{
send: (channel, data) => {
ipcRenderer.send(channel, data);
},
invoke: async (channel, data) => {
return await ipcRenderer.invoke(channel, data);
}
}
);