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

175
Views
How do I make the rows returned by a sqlite query through node.js available to a js frontend script?

I'm trying to create a simple web page to display the contents of a table stored in a sqlite3 table. I'm using vanilla js for the frontend (NO JQUERY!) and node.js + sqlite3 for the backend. I can query the database just fine within my node script but can't figure out how to return the results to my frontend js script. The problem seems to be that since the queries are asynchronous, the function ( get_rows() ) returns before the queries are complete. Code below.

main.js (frontend script used by web page)

    xmlhttp.open("POST", "http://localhost:3000/load", true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.responseText);
            let rows = JSON.parse(xmlhttp.responseText);
            console.log(rows);
            //now we can parse rows and create table
            console.log("rows: " + rows);
        }
    }
    xmlhttp.setRequestHeader("Content-Type", "text/plain", "Access-Control-Allow-Origin", "*");
    xmlhttp.send(tbl_name); 

access.js (node.js script handling sqlite3 query)

const http = require('http');
const url = require('url');
const sqlite = require('sqlite3').verbose();


 get_rows = function(table) {
    let test = null;

    //open database
    let db = new sqlite.Database("../../databases/test_db.db", err=> {
        if (err) {
            console.error(err.message);
        }
        console.log("Opened database from disk");
    });

    //get rows
    db.all('SELECT * FROM ' + table, (err, row) => {
        if (err) {
            console.error(err.essage);
        }
        test = row;
    });
    //close database
    db.close((err) => {
        if (err) {
            console.error(err.message);
        }
        console.log("Closed database");
    });

    return test;
}

http.createServer(function (req, res) {
    let path = url.parse(req.url).pathname;
    res.writeHead(200, {"Content-type": "text/plain", 'Access-Control-Allow-Origin' : '*'});

    if (path === "/load") {
        req.on("data", function(tbl_name) {
            console.log("Loaded table: " + tbl_name);
            res.end(get_rows(tbl_name));
       });
    }
}).listen(3000);
about 4 years ago · Juan Pablo Isaza
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!