diff --git a/abstract-leveldown.js b/abstract-leveldown.js index 1fcfdf9f..3e641d9d 100644 --- a/abstract-leveldown.js +++ b/abstract-leveldown.js @@ -161,11 +161,11 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { if (!options || typeof options != 'object') options = {} - var serialized = [] + var serialized = new Array(array.length) for (var i = 0; i < array.length; i++) { - if (typeof array[i] !== 'object') - continue + if (typeof array[i] !== 'object' || array[i] === null) + return callback(new Error('batch(array) element must be an object and not `null`')) var e = Object.assign({}, array[i]) var err @@ -181,7 +181,7 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { if (e.type !== 'del') e.value = this._serializeValue(e.value) - serialized.push(e) + serialized[i] = e } if (typeof this._batch == 'function') diff --git a/abstract/batch-test.js b/abstract/batch-test.js index a178dc18..61a62ceb 100644 --- a/abstract/batch-test.js +++ b/abstract/batch-test.js @@ -84,6 +84,18 @@ module.exports.args = function (test) { t.end() }) }) + + ;[null, undefined, 1, true].forEach(function (element) { + var type = element === null ? 'null' : typeof element + + test('test batch() with ' + type + ' element', function (t) { + db.batch([element], function (err) { + t.ok(err, 'got error') + t.equal(err.message, 'batch(array) element must be an object and not `null`', 'correct error message') + t.end() + }) + }) + }) } module.exports.batch = function (test) {