diff --git a/packages/node_modules/pouchdb-core/src/adapter.js b/packages/node_modules/pouchdb-core/src/adapter.js index d8b7fbe37a..808b597f18 100644 --- a/packages/node_modules/pouchdb-core/src/adapter.js +++ b/packages/node_modules/pouchdb-core/src/adapter.js @@ -190,6 +190,10 @@ function attachmentNameError(name) { return false; } +function isNotSingleDoc(doc) { + return doc === null || typeof doc !== 'object' || Array.isArray(doc); +} + function isValidRev(rev) { return typeof rev === 'string' && /^\d+-/.test(rev); } @@ -201,7 +205,7 @@ class AbstractPouchDB extends EventEmitter { callback = opts; opts = {}; } - if (typeof doc !== 'object' || Array.isArray(doc)) { + if (isNotSingleDoc(doc)) { return callback(createError(NOT_AN_OBJECT)); } this.bulkDocs({docs: [doc]}, opts, yankError(callback, doc._id)); @@ -212,7 +216,7 @@ class AbstractPouchDB extends EventEmitter { cb = opts; opts = {}; } - if (typeof doc !== 'object' || Array.isArray(doc)) { + if (isNotSingleDoc(doc)) { return cb(createError(NOT_AN_OBJECT)); } invalidIdError(doc._id); @@ -785,7 +789,7 @@ class AbstractPouchDB extends EventEmitter { } for (var i = 0; i < req.docs.length; ++i) { - if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) { + if (isNotSingleDoc(req.docs[i])) { return callback(createError(NOT_AN_OBJECT)); } } diff --git a/tests/integration/test.basics.js b/tests/integration/test.basics.js index b86187c322..b0c50ce4a3 100644 --- a/tests/integration/test.basics.js +++ b/tests/integration/test.basics.js @@ -794,23 +794,39 @@ adapters.forEach(function (adapter) { }); }); - it('Error when document is not an object', function (done) { - var db = new PouchDB(dbs.name); - var doc1 = [{ _id: 'foo' }, { _id: 'bar' }]; - var doc2 = 'this is not an object'; - var count = 5; - var callback = function (err) { - should.exist(err); - count--; - if (count === 0) { - done(); - } - }; - db.post(doc1, callback); - db.post(doc2, callback); - db.put(doc1, callback); - db.put(doc2, callback); - db.bulkDocs({docs: [doc1, doc2]}, callback); + [ + undefined, + null, + [], + [{ _id: 'foo' }, { _id: 'bar' }], + 'this is not an object', + String('this is not an object'), + //new String('this is not an object'), actually, this _is_ an object + ].forEach((badDoc, idx) => { + describe(`Should error when document is not an object #${idx}`, () => { + let db; + + const expectNotAnObject = fn => async () => { + let threw; + try { + await fn(); + } catch (err) { + threw = true; + err.message.should.equal('Document must be a JSON object'); + } + if (!threw) { + throw new Error('should have thrown'); + } + }; + + beforeEach(() => { + db = new PouchDB(dbs.name); + }); + + it('should error for .post()', expectNotAnObject(() => db.post(badDoc))); + it('should error for .put()', expectNotAnObject(() => db.put(badDoc))); + it('should error for .bulkDocs()', expectNotAnObject(() => db.bulkDocs({docs: [badDoc]}))); + }); }); it('Test instance update_seq updates correctly', function (done) {