Skip to content

Commit

Permalink
fix(bulkWrite): fix issue with bulkWrite continuing w/ callback
Browse files Browse the repository at this point in the history
  • Loading branch information
daprahamian committed Apr 1, 2019
1 parent b86ee69 commit 2a4a42c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions lib/bulk/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,15 @@ class BulkOperationBase {
);
}

_handleEarlyError(err, callback) {
if (typeof callback === 'function') {
callback(err, null);
return;
}

return this.s.promiseLibrary.reject(err);
}

/**
* Execute next write command in a chain
*
Expand All @@ -919,19 +928,17 @@ class BulkOperationBase {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

if (this.s.executed) {
const executedError = toError('batch cannot be re-executed');
return typeof callback === 'function'
? callback(executedError, null)
: this.s.promiseLibrary.reject(executedError);
}

if (typeof _writeConcern === 'function') {
callback = _writeConcern;
} else if (_writeConcern && typeof _writeConcern === 'object') {
this.s.writeConcern = _writeConcern;
}

if (this.s.executed) {
const executedError = toError('batch cannot be re-executed');
return this._handleEarlyError(executedError, callback);
}

// If we have current batch
if (this.isOrdered) {
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
Expand All @@ -943,9 +950,7 @@ class BulkOperationBase {
// If we have no operations in the bulk raise an error
if (this.s.batches.length === 0) {
const emptyBatchError = toError('Invalid Operation, no operations specified');
return typeof callback === 'function'
? callback(emptyBatchError, null)
: this.s.promiseLibrary.reject(emptyBatchError);
return this._handleEarlyError(emptyBatchError, callback);
}
return { options, callback };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bulk/ordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class OrderedBulkOperation extends BulkOperationBase {
*/
execute(_writeConcern, options, callback) {
const ret = this.bulkExecute(_writeConcern, options, callback);
if (isPromiseLike(ret)) {
if (!ret || isPromiseLike(ret)) {
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/bulk/unordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class UnorderedBulkOperation extends BulkOperationBase {
*/
execute(_writeConcern, options, callback) {
const ret = this.bulkExecute(_writeConcern, options, callback);
if (isPromiseLike(ret)) {
if (!ret || isPromiseLike(ret)) {
return ret;
}

Expand Down

0 comments on commit 2a4a42c

Please sign in to comment.