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 6058423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:35:52+00:00 2026-05-23T08:35:52+00:00

I have a http server setup which basically needs to look up stuff in

  • 0

I have a http server setup which basically needs to look up stuff in the database.

Here is the code snippet :

var sys = require('sys');
var Client = require('mysql').Client;
var client = new Client();

client.host = '_';
client.user = '_';
client.password = '_';
client.database = '_';
var http = require('http');
http.createServer(function(req, res) {
    req.on('end', function() {
        client.connect(function(error, results) {
            if (error) {
                console.log('Connection Error:');
                return;
            }
            ClientConnectionReady(client);
        });
        ClientConnectionReady = function(client) {
            var final = '';
            client.query('select * from table', function selectCb(error, result, fields) {
                if (error) {
                    console.log('ERROR');
                    client.end();
                    return;
                }
                final += "{" + JSON.stringify(result);

            });
            client.query("SELECT    COUNT(*) from table", function selectCb(error, result, fields) {
                if (error) {
                    console.log('ERROR');
                    client.end();
                    return;
                }
                final += "," + JSON.stringify(result) + "}";
            });
            res.writeHead(200, {
                'Content-Type': 'text/plain'
            });
            res.write(final);
            res.end();
            client.end();
        };
    });
}).listen(8007, "127.0.0.1");
  

If I print the values of the variable ‘final’ at the places where I assign them, I see valid values, but at the lines when I do ‘res.write(final)’, final is still blank.

How do I make this work and why is this failing?? Thanks for the help, I am new to node.js

  • 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-05-23T08:35:53+00:00Added an answer on May 23, 2026 at 8:35 am

    The Node.js environment is asynchronous. Those statements that modify “final” are inside callbacks that are executed only when the database operations finish. The code immediately after the initiation of the database operations, where you write the result, are executed long before those callbacks run.

    You’ve almost stumbled upon the answer to the problem already: you must not write the result until the operations are finished, which you know will be the case inside the callbacks. If you must wait for both to finish (seems like you do), then you can do something like keep a counter in the outer scope. Each callback can increment the counter, and call the same result-writer function only when the counter indicates that both callbacks are complete. (I have the idea that the Node runtime has a fancier way of doing that sort of thing, but I’m not that familiar with it. In a simple case like this, keeping something like a counter is easy enough to do.)

    Also, an unrelated note: that “ClientConnectionReady” variable should probably either be written as a function definition:

    function ClientConnectionReady(client) {
      // ...
    }
    

    or else it should be declared with var. (I’m a little surprised in fact that it’s not throwing an error, but again I’m not that familiar with Node.js.)

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

Sidebar

Related Questions

I have an HTTP server which is in our internal network and accessible only
I have a HTTP Live streaming server setup that serves segmented mp3 audio content
I have an HTTP server that returns large bodies in response to POST requests
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
I have a base URL : http://my.server.com/folder/directory/sample And a relative one : ../../other/path How
I have a server access log, with timestamps of each http request, I'd like
I'd like to have one server make an http post request to another server.
I have URLs of the form http://domain/image/⟨uuid⟩/42x42/some_name.png . The Web server (nginx) is configured
I have a listview that is setup like so: <asp:ListView ID=lv_First runat=server> <LayoutTemplate> <div
I have a http:// site that needs to access a 3rd party JSON API

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.