#SQLite query (db.all(err,res)) returns results to terminal but undefined in React component

76 messages · Page 1 of 1 (latest)

calm sentinel
#

show your code for ipc

wraith hemlock
#

If you ping your code what you have written we can help you with it..

fathom relic
#

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
}
})

})

fathom relic
#

//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 ()=>{}

}, []);

calm sentinel
#

But you didnt return any value

fathom relic
calm sentinel
#

It doesnt work because you cant return value from callback without promise wrapper

#

Return goes into the void

fathom relic
calm sentinel
#

Api you use doesn't return anything

#

on/send is throw and forget

#

handle/invoke is request/response

#

Open link from above with example

fathom relic
#

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); } }) })**

calm sentinel
#

yes

fathom relic
# calm sentinel 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

calm sentinel
#

No

#

It's not react

fathom relic
fathom relic
# calm sentinel It's not react

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'}]
})

calm sentinel
#

So?

#

Do same with promise

fathom relic
# calm sentinel So?

// returns undefined
const obj =[{name:"john snow"}]
new Promise((resolve,reject)=>{
resolve(obj)
})

fathom relic
# calm sentinel Do same with promise

//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.

},
calm sentinel
#

Show me complete ipcMain.handle code

fathom relic
#

//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)
})

calm sentinel
#

Its not complete code

fathom relic
calm sentinel
#

only ipcMain.handle('get-all-users'

#

the one you show have error

fathom relic
#

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);
}
})
})
})

calm sentinel
#

there is no return for handle function

fathom relic
calm sentinel
#

yes

#

you did it wrong

#

promise dont care about return

#

but i am talked about function for ipcMain.handle function

calm sentinel
#

return attached to a function you call it from

fathom relic
calm sentinel
#

how familiar you with js?

fathom relic
calm sentinel
#

and async/await specifically

fathom relic
calm sentinel
#

how callbacks work in js?

fathom relic
calm sentinel
#

ok

fathom relic
calm sentinel
#

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?

fathom relic
calm sentinel
#

what data?

fathom relic
#

from doRequest

calm sentinel
#

and what value in it?

fathom relic
#

Hello

calm sentinel
#

no

fathom relic
#

success

calm sentinel
#

no

fathom relic
#

undefined

calm sentinel
#

do you know why?

fathom relic
#

no parameter?

calm sentinel
#

i mean why it's return undefined

fathom relic
#

its not resolved

calm sentinel
#

because doRequest return undefined

#

i can show you the code you need

fathom relic
calm sentinel
#

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);
            }
        })
    })
})
fathom relic
#

bro! it works now, thanks a lot