Skip to content

Commit

Permalink
create all docs before calling back
Browse files Browse the repository at this point in the history
Resolves nightscout#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.)
  • Loading branch information
dduugg committed Mar 1, 2015
1 parent d616b6c commit d7d917b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d7d917b

Please sign in to comment.