'Returning values from a Promise to a React Component using Electron and contextBridge and sqlite3
I am trying to connect a SQLite3 database with a react component using electron contextBridge and continue to return a promise {<pending>} in my frontend react component. I have rapped my SQL call in a new Promise.
Quotes_mgr.js
exports.getActiveQuoteData = () => {
return new Promise(( resolve , reject ) => {
db.serialize(() => {
db.all(
"SELECT * FROM Quotes ",
(err, rows) => {
if (err) reject(err);
resolve(rows);
}
);})
})
};
This function is called with electrons ipcMain.handle in my main.js file.
main.js
ipcMain.handle("get_active_quotes", (event) => {
return Quotes_mgr.getActiveQuoteData();
//return Employee_mgr.getEmployeeid(args);
});
through the contextBridge api in the preload.js file.
preload.js
contextBridge.exposeInMainWorld("api", {
on(eventName, callback) {
ipcRenderer.on(eventName, callback);
},
getActiveQuotes: () => ipcRenderer.invoke("get_active_quotes"),
});
Then returns the SQL results to my React component.
index.jsx
const getInStore_List = async () => {
return await window.api.getActiveQuotes();
}
const InStore_List = getInStore_List()
InStore_List returns a promise {<pending>} containing the SQL results but I need it to return the SQL results so i can use them in my React component. I am new to Promises and Async functions and believe that the problem is in my new Promise implantation but have not been able to figure out why this is not working. Any help would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
