Q:

insert error using node.js async series

I have a for loop that inserts documents in MongoDB. Using async series I run the loop five times then close the connection. For some strange reason I get an insert error when the loop is executed for the sixth time. I am just not sure what is happening here. Any ideas? 
var mycollection= "abcd";
var count = 5;
var db;
var col;

async.series([
 // Connect to DB
 function(callback) {
  MongoClient.connect("mongodb://connection-path-here",function(error, db2) {
      if (error) {console.log("db connect error");callback(error,"db connect error"); return;} 
      db = db2;
      callback(null,"connect success");
  });
},
function(callback) {
  col = db.collection(mycollection);
  callback(null,"collection success");
},
function(callback) {
  console.log ("insert begin ...");               
  for (var i = 1; i <= count; i++) {
    console.log("inserting ....." + i);
    col.insert({c:i}, function(error,result) {
        if (error) {console.log("insert error:" + i);callback(error,"insert error"); return;}
    });
  }
callback(null,"insert success");
},
function (callback){
  console.log ("close db");db.close();
  callback(null,"connection closed");
}
], function(error, results) {
    if (error) { console.log("error"); }
        console.log(results);
});

output
insert begin ...
inserting .....1
inserting .....2
inserting .....3
inserting .....4
inserting .....5
close db
insert error:6
error
  [ 'connect success', 'collection success', 'insert error' ]
insert error:6
insert error:6
insert error:6
insert error:6

A:

You started well but you are mixing async and non-async methods here:
function(callback) {
  console.log ("insert begin ...");               
  for (var i = 1; i <= count; i++) {
    console.log("inserting ....." + i);
    col.insert({c:i}, function(error,result) {
        if (error) {console.log("insert error:" + i);callback(error,"insert error"); return;}
    });
  }
callback(null,"insert success");
},

This does not wait for the insert to complete before iterating the next loop, so you need something that waits. Try "async.whilst":
function(callback) {
    var i = 1;
    async.whilst(
        function() { return i <= count },
        function(callback) {
            col.insert({ c: i },function(error,result) {
                if (error)
                    console.log("insert error:" + i);
                i++;
                callback(error);
            });
        },
        function(error) {
          callback(error,"insert sucess")
        }
    );
},

The rest can proceed as you are doing and the results will be sent to the end of the series execution.

