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

96
Views
Electron await sqlite3 response

I have a sqlite3 database request in my main.js, that is triggered by button click in renderer.js.

The request reaches my main.js. However, I cannot manage to await the results from the database. The issue occurs already in main.js, so I'm stuck even before anything is passed back to the renderer.js.

I hope someone can tell me what I am missing.

Here is my code:

renderer.js

$(document).on('click','#mybtn',function(e){

    let query = "SELECT id, name FROM table1"

    // send (here is the issue)
    window.api.send("db-query", query)

    // (next step: receive, might be wrong but not yet my problem)
    window.api.receive(channel="receive-db-data", (data) => {
      console.log(data);
    });

});

main.js


ipcMain.on(channel='db-query', async (e, query) => {
  console.log('query received: ' + query);
  
  let data = await db_request(query).then(
      function(value) {
         console.log('value: ' + value);
         return value;
      },
      function(error) {
         console.log('error fetching data from db on query:' + query);
      }
  )
  console.log("response ready: " + data); //returns undefined if 'return value' is used (otherwise nothing)

  // to send back to renderer.js later
  e.sender.send("db-data", data)

})


let db_request = async (query) => {

  let data = []

  var sqlite3 = require('sqlite3').verbose();
  var dbPath = require('path').resolve(__dirname, '../../Fin.db')
  var db = new sqlite3.Database(dbPath)

  db.serialize(function(){
    db.each(query, function(err, row) {
      console.log(row)
      data.push({"id": row.id, "name": row.name})
    });
  });
  db.close();

  console.log('db_request:' + data)

  return data
}

And this is how my terminal looks like:

query received: SELECT id, type, name FROM table1
db_request:
value: 
response ready: undefined
{ id: 1, name: 'a' }
{ id: 2, name: 'b' }
{ id: 3, name: 'c' }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to convert db_request result to a Promise, and the promise will be resolved when all rows are pushed to the data. When you use the await keyword, there is no need to handle a promise with .then chain.

main.js will look like this:

const sqlite3 = require('sqlite3').verbose();
const dbPath = require('path').resolve(__dirname, '../../Fin.db')

ipcMain.on(channel='db-query', async (e, query) => {
  console.log('query received: ' + query)

  try {
    const data = await db_request(query); // remove .then
    console.log('value: ' + data)
    // to send back to renderer.js later
    e.sender.send("db-data", data)
  } catch (error) {
    console.log('error fetching data from db on query:' + query);
    e.sender.send("db-data", []) // send empty data or error ???
  }
})


let db_request = (query) => {
  const db = new sqlite3.Database(dbPath)

  return new Promise((resolve, reject) => { // return a promise
    // I think you dont need serialize for this case
    const data = []
    db.each(query, (err, row) => {
      console.log(err, row)
      if (!err) {
        data.push({"id": row.id, "name": row.name})
      }
    }, (error) => {
      if (error) {
        reject(error)
      } else {
        resolve(data)
      }
    });
  })
}
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!