Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:16:57+00:00 2026-06-17T15:16:57+00:00

I’m using Node.js + Express + nodejs-sqlite3 to make a form that when submited

  • 0

I’m using Node.js + Express + nodejs-sqlite3 to make a form that when submited will insert a new row on an slite3 database.
On query sucess I want to write certain response.

So the small big problem is just: Modify a string that will be storing the html to be shown, inside the callback function of sqlite3.run()

I read about closures, and passing an object with methods to modify its own attributes. But it seems it’s not working. It will pass the object attributes and methods, but no change will remain when the callback function ends. I read that objects will be passed as reference, not copies.

This is the code:

app.post("/insert.html", function(req, res){
    function TheBody(){
        this.html = "";
        this.msg = "";
        this.num = "";
    }
    TheBody.prototype.add = function(string){
        this.html = this.html + string;
    }
    var body = new TheBody();

    body.msg = req.body.message;
    body.num = req.body.number;

    var insertCallback = function(data){
        return function(err){
            if( err != null){
                console.log("Can't insert new msg: " + err.message);
                data.add("ERROR-DB");                                               
            } else {
                console.log("Ok. Inserted: " + data.msg);
                console.log(data.html);
                data.add("OK - MSG: "+data.msg+" NUM: "+data.num);
                console.log(data.html);
            }
        };
    };                                                                                   

    var db = new lite.Database('database.db');
    var query = "INSERT INTO outbox (message, number) VALUES (?, ?)";
    db.run(query, [body.msg, body.num], insertCallback(body) );

    res.setHeader('Content-Type', 'text/html');
    res.setHeader('Content-Length', body.html.length);
    res.end(body.html);
}

On server side I’ll see

Ok. Inserted: TestString
[Blank space since data.html still has no information]
OK - MSG: TestString NUM: TestNumber [Showing that indeed was modified inside the function]

But on the client side res.end(body.html); will send an empty string.
The object is not being passed as reference.

What’s missing in the code, and what simpler alternatives I have to change a string variable inside a callback anonymous function?.

I already know I could use response.write() to write directly on the function if it were more simpler. But I discovered it would only work if I use response.end() inside the callback, otherwise (being outside as it is now) it will meet a race condition where the buffer will be closed before sqlite3.run() be able to use response.write().

——– Answered ——–

As hinted by Justin Bicknell and confirmed by George P. Nodejs-sqlite3 functions are run asynchronously. So I was ending the stream to the client before the callback would be called, thus nothing was being printed.
This was a problem more about “This is SPART- nodejs, so write your stuff according to events'” rather than a logic one. I found this kind of programming kind of convoluted but nobody else than me told me to use nodejs. For those wondering about how one could put some order over the order of queries on the database, nodejs-sqlite3 functions returns a database object that is used to chain the next query.

Since I was printing the information to the client just once in every handled event, the resulting object ended like this:

function TheBody(response){
    this.response = response;
}
TheBody.prototype.printAll = function(string){
    this.response.setHeader('Content-Type', 'text/html');
    this.response.setHeader('Content-Length', string.length);
    this.response.end(string);
} 

Preferring that to clutter all the code a lot of res.setHeader() lines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:16:58+00:00Added an answer on June 17, 2026 at 3:16 pm

    node-sqlite3 methods are, by default, run in parallel (asynchronously). That means that your code is going through this timeline:

    1. Your code calls db.run(...)
    2. Your code calls res.end(...)
    3. db.run completes and calls your callback.

    This is the source of a huge number of questions here on SO, so you can almost certainly find a better answer than anything that I could write here in a reasonable amount of time.

    I would start here: How does Asynchronous Javascript Execution happen? and when not to use return statement?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.