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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:45:13+00:00 2026-06-03T10:45:13+00:00

I’m using the node-postgres module for node.js and I have a problem inserting data.

  • 0

I’m using the node-postgres module for node.js and I have a problem inserting data.

The function:

function addItems(listId, listItems, handle) {
    if (!listItems) {
        handle(listId);
        return;
    }
    var client = dbConnector.getClient(),
        important,
        dateArray,
        dateString,
        i,
        prepStatement;
    client.connect();
    for (i in listItems) {
        console.log(listItems[i]);
        dateArray = listItems[i].itemdate.split('-');
        dateString = dateArray[1] + '-' + dateArray[0] + '-' + dateArray[2];
        if (listItems[i].important) {
            important = 'true';
        } else {
            important = 'false';
        }
        prepStatement = {
            name: 'insert task',
            text: 'INSERT INTO listitem (todolist_id, name, deadline, description, important, done, created) VALUES ($1, $2, $3, $4, $5, $6, now()) RETURNING listitem_id',
            values: [ listId, listItems[i].itemname, dateString, listItems[i].itemdesc, important, listItems[i].done ]
        };
        var query = client.query(prepStatement);
        console.log("Adding item " + i);
        query.on('error', function(error) {
            console.log(error);
        });
        query.on('end', function(result) {
           console.log("Query ended");
           if (result) {
               console.log("Added listitem no " + result.rows[0].listitem_id);
           } 
        });
    }
    client.end();
    handle(listId);
}

No new data appears in the database. The query.on('error') and query.on('end') events are never fired. Come to think of it, I’m beginning to doubt if the query is even triggered (tho I can’t see why it shouldn’t).

The only log I get is:

{  itemname: 'Task 1',
   itemdate: '08-05-2012',
   important: 'on',
   itemdesc: 'A task',
   done: 'false' }
Adding item 0
{  itemname: 'Task 2',
   itemdate: '22-05-2012',
   important: 'on',
   itemdesc: 'Another one',
   done: 'false' }
Adding item 1

So how should I proceed in debugging 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-03T10:45:15+00:00Added an answer on June 3, 2026 at 10:45 am

    Your calling client.end() before your queries can execute. node-postgres is not going to throw a “not connected” error, because it is designed to queue queries until the connection is ready. https://github.com/brianc/node-postgres/wiki/Client#method-connect

    Try this:

    function addItems(listId, listItems, handle) {
        if (!listItems) {
            handle(listId);
            return;
        }
        var client = dbConnector.getClient(),
            important,
            dateArray,
            dateString,
            i, 
            prepStatement,
            queryCount = 0;
        client.connect();
        for (i in listItems) {
            console.log(listItems[i]);
            dateArray = listItems[i].itemdate.split('-');
            dateString = dateArray[1] + '-' + dateArray[0] + '-' + dateArray[2];
            if (listItems[i].important) {
                important = 'true';
            } else {
                important = 'false';
            }
            prepStatement = {
                name: 'insert task',
                text: 'INSERT INTO listitem (todolist_id, name, deadline, description, important, done, created) VALUES ($1, $2, $3, $4, $5, $6, now()) RETURNING listitem_id',
                values: [ listId, listItems[i].itemname, dateString, listItems[i].itemdesc, important, listItems[i].done ]
            };
            var query = client.query(prepStatement);
            queryCount++;
            console.log("Adding item " + i);
            query.on('error', function(error) {
                console.log(error);
            });
            query.on('end', function(result) {
               queryCount--;
               console.log("Query ended");
               if (result) {
                   console.log("Added listitem no " + result.rows[0].listitem_id);
               } 
               if (queryCount === 0) {
                 client.end();
                 handle(listId);
               }
            });
        }
    }
    

    All the above does is keep track of the number of queries you’ve issued and when they have all ended, then calls client.end() and handle(listId);

    This can be tedious and error prone, thus a few libraries exist to make asyc flow easier. My favorite is async, it works in the browser and in node. https://github.com/caolan/async

    Using async, I would rewrite the code as:

    function addItems(listId, listItems, handle) {
        if (!listItems) {
            handle(listId);
            return;
        }
        var client = dbConnector.getClient(),
            important,
            dateArray,
            dateString,
            i, 
            prepStatement;
        client.connect();
    
        async.forEach(
          listItems,
          // called for each listItems
          function(listItem, callback){
            console.log(listItem);
            dateArray = listItem.itemdate.split('-');
            dateString = dateArray[1] + '-' + dateArray[0] + '-' + dateArray[2];
            if (listItem.important) {
                important = 'true';
            } else {
                important = 'false';
            }
            prepStatement = {
                name: 'insert task',
                text: 'INSERT INTO listitem (todolist_id, name, deadline, description, important, done, created) VALUES ($1, $2, $3, $4, $5, $6, now()) RETURNING listitem_id',
                values: [ listId, listItem.itemname, dateString, listItem.itemdesc, important, listItem.done ]
            };
            var query = client.query(prepStatement);
            //console.log("Adding item " + i);
            query.on('error', function(error) {
                console.log(error);
                callback(error),
            });
            query.on('end', function(result) {
               console.log("Query ended");
               if (result) {
                   console.log("Added listitem no " + result.rows[0].listitem_id);
               } 
               callback(null,result);
            });
          }, 
          // called after iterator function
          function(err) {
            if (err) return; // could use this as an err handler for all queries              
            client.end();
            handle(listId);
          }
        );
    }; 
    

    see also async.forEachSeries, but I don’t think it’s needed in this case because the node-postgres client is going to run the queries in series anyways.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.