Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Why is my ipcMain returning an empty array after reading through a database?

For my project, I am using electron and react with ipcRenderer and ipcMain to communicate with a database. I can see it go through the database, but it returns an empty array. It is like the array is returned before anything is read from the db.

Here is the code I am using in my ipc main: I am expecting it to return the names of the categories, but all it returns cateNames blank.

ipcMain.on(channels.GET_CATS, async(event,type) => {
    console.log("made it")
    let cateNames=[];

    db.each(`SELECT name FROM categories WHERE type=?`, [type],(err, row) => {
        if (err) {
            return console.error(err.message);
        }

        console.log(row.name);
        cateNames.push(row.name);
    });
    
    console.log(cateNames);
    event.sender.send(channels.GET_LOGIN, cateNames);
});

I send the request with ipcRenderer.send(channels.GET_CATS,"Donation"); with an ipcRenderer.on listening that will output the array to the console.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Javascript / Node.js execute commands in sequential order and does not "wait" when moving from one command to the next unless explicitely told to via the use of promises and async / await commands.

The reason your current code returns an empty cateNames array is becasue execution of the code does not "wait" for the db.each command to return it's callback. The callback is only returned once the DB has someting to return, which will either be a row or an error. This takes time. In the meantime, execution has moved on the next command.

To make this block of code "wait" until the DB has returned all available rows (if any) we could to use promises.

Instead, I propose a simpler method. Instead of pushing row.name with every db.each iteration, just use db.all and craft the response afterwards.

ipcMain.on(channels.GET_CATS, async(event, type) => {
    console.log("made it")
    let cateNames = [];

    db.all(`SELECT name FROM categories WHERE type=?`, [type], (err, rows) => {
        if (err) {
            return console.error(err.message);
        }

        for (let row of rows) {
            cateNames.push(row.name);
        }
    
        console.log(cateNames);

        // Use event.reply(channel, data);        
        event.reply(channels.GET_LOGIN, cateNames);
    });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!