From d7d917b03b0851db0b02e1fd273b1e0f4d624325 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Tue, 17 Feb 2015 22:33:16 -0800 Subject: [PATCH] create all docs before calling back Resolves #423. Also changes the callback signature, invoking it with the error and docs, rather than the error, totalCreated, and docs. The third paramter isn't used, and the second parameter should be an array. (Since totalCreated was 0 on callback, it was falsy and replaced with an empty array when used in format_entries.) --- lib/entries.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/entries.js b/lib/entries.js index c8f8eb7a64b..fa83324e304 100644 --- a/lib/entries.js +++ b/lib/entries.js @@ -96,21 +96,23 @@ function storage(name, storage, pushover) { // store new documents using the storage mechanism function create (docs, fn) { - with_collection(function(err, collection) { - if (err) { fn(err); return; } - // potentially a batch insert - var firstErr = null, - totalCreated = 0; - - docs.forEach(function(doc) { - collection.update(doc, doc, {upsert: true}, function (err, created) { - firstErr = firstErr || err; - totalCreated += created; - }); - sendPushover(doc); + with_collection(function(err, collection) { + if (err) { fn(err); return; } + // potentially a batch insert + var firstErr = null, + numDocs = docs.length, + totalCreated = 0; + + docs.forEach(function(doc) { + collection.update(doc, doc, {upsert: true}, function (err, created) { + firstErr = firstErr || err; + if (++totalCreated === numDocs) { + fn(firstErr, docs); + } }); - fn(firstErr, totalCreated, docs); + sendPushover(doc); }); + }); } //currently the Android upload will send the last MBG over and over, make sure we get a single notification