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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:23:29+00:00 2026-06-15T21:23:29+00:00

So I am making my first attempt with Node and I can’t really wrap

  • 0

So I am making my first attempt with Node and I can’t really wrap my head around how to work with the MySQL connection. The script is somewhat simplified like this

var mysql       = require('mysql');
var connection  = mysql.createConnection({
  host     : '192.168.40.1',
  user     : 'user',
  password : 'password',
  database : 'database'
});

function DoSomething(connection, item, callback) {
    connection.query(
        'SELECT COUNT(*) AS count FROM another_table WHERE field=?', 
        item.field, 
        function (err, results) {
            if (err) throw err;

            if (results.length > 0 && results[0].count >= 1) {
                callback(err, connection, item, 'Found something')
            }
    });
}

function DoSomethingElse(connection, item, callback) {
    // Similar to DoSomething()
}

function StoreResult(err, connection, item, reason) {
    if (err) throw err;

    connection.query(
        'INSERT INTO result (item_id, reason) VALUES (?, ?)',
        [item.id, reason],
        function (err, results) {
            if (err)  throw err;
    });
}

connection.query('SELECT * FROM table WHERE deleted=?', [0], function (err, results) 
{
    if (err) throw err;

    results.forEach(function (item, index) {
        DoSomething(connection, item, StoreResult);
        DoSomethingElse(connection, item, StoreResult);
    });
});

connection.end();

What I am having trouble with (as far as I can tell) is that since DoSomething() it seems that connection.end() is called before all of the DoSomething()‘s have finished causing errors that queries can’t be performed when the connection is closed.

I tried playing around with the async library, but I haven’t gotten anywhere so far. Anyone with some good advice on how to do this?

  • 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-15T21:23:30+00:00Added an answer on June 15, 2026 at 9:23 pm

    The problem with your code is that you’re closing the connection synchronously while an asynchronous request is still being handled. You should call connection.end() only after all query callbacks have been called.

    Since you are doing multiple queries, this means using some way to wait for all their results. The simplest way is to nest every next call into the callback of the previous one, but that way leads to the pyramid of doom. There are a number of popular libraries that solve this, but my own preference is for async.

    Using async I would rewrite your code as follows:

    async.waterfall([function(next) {
      connection.query('SELECT * FROM table WHERE deleted=?', [0], next); // note the callback
    },
    function(results, next) {
      // asynchronously handle each results. If they should be in order, use forEachSeries
      async.forEach(results, function(item, next) {
        // handle in parallel
        async.parallel([function(done) {
          DoSomething(connection, item, function(err, connection, item, reason) {
            // This is a hack, StoreResult should have a callback which is called
            // after it's done, because the callback is now being called too soon
            StoreResult(err, connection, item, reason);
            callback(err);
          });
        }, function(done) {
          DoSomethingElse(connection, item, function(err, connection, item, reason) {
            // This is a hack, StoreResult should have a callback which is called
            // after it's done, because the callback is now being called too soon
            StoreResult(err, connection, item, reason);
            callback(err);
        }], function(err) {
          // this callback is called with an error as soon as it occurs
          // or after all callbacks are called without error
          next(err);
        });
      }, function(err) {
        // idem
        next(err);
      });
    }], function(err, results) {
      // idem
      // Do whatever you want to do with the final error here
      connection.end();
    });
    

    This also allows you to solve a possible issue with the order of your queries in the forEach: They are started in order, but are not guaranteed to finish in order due to their asynchronous nature.

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

Sidebar

Related Questions

I am making an attempt at my first powershell script and getting a bad
I'm making my first attempt at experimenting with Comet. I developed a very simple
For my first attempt at programming, I am making a simple webapp with php
I've been making a first attempt to use fluent nHibernate on an ASP.NET MVC
I'm making a first attempt at embedding an mp3 file on a webpage. I've
I am making a first attempt at playing with the new Tasks, but something
I'm new to making games with pygame and my first attempt is still a
I'm an old C++ / algorithm guy who is making a first attempt at
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
I'm making my first iphone application and it's a wine chart application. It's with

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.