#SQLite query (db.all(err,res)) returns results to terminal but undefined in React component
76 messages · Page 1 of 1 (latest)
If you ping your code what you have written we can help you with it..
sure
ipcMain.on('get-all-users', async (event, args) => {
//fetchUsers();
db.all(SELECT * FROM accounts ORDER BY id DESC, (error, result) => {
if (error) console.error('Error : ', error)
if (result) {
console.log(result);
return result;
} else {
return null
}
})
})
i have this in electron.js
//preload.js
const { ipcRenderer, contextBridge } = require('electron');
//-------------------------------------------------------
contextBridge.exposeInMainWorld('api', {
//-------------------------USER---------------------------------------------
getAllUsers: (args)=> ipcRenderer.send('get-all-users', args),
});
...and in react component i'm calling like so
useEffect(() => {
async function findUsers(){
const data = await window.api.getAllUsers()
if(data){
console.log(data)
}
console.log(data)
}
findUsers()
return ()=>{}
}, []);
But you didnt return any value
yes i've tried returning it's still same
Use the ipcMain and ipcRenderer modules to communicate between Electron processes
It doesnt work because you cant return value from callback without promise wrapper
Return goes into the void
i've also tried wrapping with promise, thing is i'm able to do console.log and i see the values but returns undefined in react
Api you use doesn't return anything
on/send is throw and forget
handle/invoke is request/response
Open link from above with example
thank you, let me try that again
is this the right way to wrap with Promise?
**ipcMain.on('get-all-users', async (event, args) => { new Promise((resolve,reject)=>{ db.all(SELECT * FROM accounts ORDER BY id DESC, (error, result) => { if (error) reject('Error : ', error) if (result) { console.log(result); return resolve(result); } }) })**
yes
or is the issue with the React component bcos if i define an object with values i get the result alright but can't get results from Promises/callbacks
I've read the IPC docs very well i must say , don't know what i'm missing
i can do something like below and get that array alright in react
ipcMain.handle('get-all-users', async (event, args) => {
return [{name:'Jon snow'}]
})
// returns undefined
const obj =[{name:"john snow"}]
new Promise((resolve,reject)=>{
resolve(obj)
})
//my webPreference
webPreferences: {
// The preload file where we will perform our app communication
preload: isDev
? path.join(app.getAppPath(), './public/preload.js') // Loading it from the public folder for dev
: path.join(app.getAppPath(), './build/preload.js'), // Loading it from the build folder for production
worldSafeExecuteJavaScript: true, // If you're using Electron 12+, this should be enabled by default and does not need to be added here.
contextIsolation: true, // Isolating context so our app is not exposed to random javascript executions making it safer.
},
Show me complete ipcMain.handle code
//the commented part is what i intend to achieve the other promise block is for testing
ipcMain.handle('get-all-users', async (event, args) => {
/* new Promise((resolve,reject)=>{
db.all(SELECT * FROM accounts ORDER BY id DESC, (error, result) => {
if (error) reject('Error : ', error)
if (result) {
console.log(result);
return resolve(result);
}
})
})*/
const obj =[{name:"john snow"}]
new Promise((resolve,reject)=>{
console.log(obj)
return resolve(obj)
})
Its not complete code
thats ipcMain.handle code or you mean the whole main.js code?
ok a sec
ipcMain.handle('get-all-users', async (event, args) => {
new Promise((resolve,reject)=>{
db.all(SELECT * FROM accounts ORDER BY id DESC, (error, result) => {
if (error) reject('Error : ', error)
if (result) {
console.log(result);
return resolve(result);
}
})
})
})
there is no return for handle function
the return is inside the Promise callback function, or did i do it wrong?
yes
you did it wrong
promise dont care about return
but i am talked about function for ipcMain.handle function
this code have it
this one dont
return attached to a function you call it from
so please how do i go about it?
how familiar you with js?
make a scope variable and assign result to it?
and async/await specifically
not in depth
how callbacks work in js?
yes
ok
so??
function doRequest(callback) {
setTimeout(()=>{
callback("Hello")
}, 1000)
}
function processRequest(){
let result = "Unknown"
result = doRequest((data)=>{
result = data;
return "Success";
})
return result;
}
question what function processRequest() return in the end?
data
what data?
from doRequest
and what value in it?
Hello
no
success
no
undefined
do you know why?
no parameter?
i mean why it's return undefined
its not resolved
ok it's not returning anything?
but its not only question you will have
ipcMain.handle('get-all-users', async (event, args) => {
return new Promise((resolve,reject)=>{
db.all('SELECT * FROM accounts ORDER BY id DESC', (error, result) => {
if (error) reject('Error : ', error)
if (result) {
console.log(result);
resolve(result);
}
})
})
})
bro! it works now, thanks a lot