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.)
This commit is contained in:
Douglas Eichelberger
2015-03-01 12:11:53 -08:00
parent d616b6c074
commit d7d917b03b
+14 -12
View File
@@ -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;
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;
totalCreated += created;
});
sendPushover(doc);
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