Reputation: 451
I check if a specific key value pair exists and insert if they don't exist. For some strange reason MongoDB throws an error during insert. Any help would much appreciated.
Update #2: Added entire function.
{
var connectURL = "mongodb://something";
var mycollection= "something2";
var db;
var col;
async.series([
// Connect to DB
function(callback) {
MongoClient.connect(connectURL,function(error, db2) {
if (error) {console.log("db connect error" + 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 ...");
var i = 1;
async.whilst(
function() { return i <= count },
function(callback) {
var mydocument = rows.shift();
col.findOne({ "sha" : mydocument.sha}, function(err, doc) {
console.log ("checked ....", mydocument.sha);
if(doc != null) {
console.log ("Exist :" + mydocument.sha);
} else {
console.log ("Inserting : " + mydocument.sha);
col.insert(mydocument,function(error,result) {
if (error) {
console.log("insert error:" + error);
callback(error);
return;
}
i++;
console.log ("inserted ...");
}); //end insert
} //end else
i++;
});//end findOne
callback(null);
},
function(error) {
callback(error,"insert sucess")
}
); //end async.whilst
},
function (callback){
//console.log ("###########close db");
db.close();
console.log("## end insert: "+ moment().format());
callback(null,"connection closed");
}
], function(error, results) {
if (error) { console.log("error"); }
//console.log(results);
});
}
Output
checked .... 078d40cc537de96310e945a50a60b0084e21d2e1
Inserting : 078d40cc537de96310e945a50a60b0084e21d2e1
insert error:Error: Connection was destroyed by application
TypeError: Cannot read property 'sha' of undefined
Upvotes: 0
Views: 2052
Reputation: 90
Denote cb1 as the callback of col.findOne
, and cb2 as the callback of col.insert
.
cb1 returns once it issues col.insert
without waiting cb2 to finish. Since cb1 also increment i and your testing function provided to async.whilst
would then evaluate to be false, which cause the async.whilst
thought it has finished all iterations.
So async.series
moves on to the next task, which then issues db.close
. Therefore the database connection may be closed before the remaining insertion commands are finished.
By the way, it seems your code snippet would jam the database by repeatedly issue col.findOne
. Your i increases in either cb1 or cb2, but the async.whilst
may repeatedly do the testing and execution many many times before each time i can be incremented. Perhaps you could use setTimeout(callback,...)
instead of calling the callback immediately.
Upvotes: 1