diff --git a/lib/drivers/node-mongodb-native/collection.js b/lib/drivers/node-mongodb-native/collection.js index 6f5a97037d5..2fb6804c3f4 100644 --- a/lib/drivers/node-mongodb-native/collection.js +++ b/lib/drivers/node-mongodb-native/collection.js @@ -208,8 +208,14 @@ function iter(i) { if (debug) { if (typeof debug === 'function') { + let argsToAdd = null; + if (typeof args[args.length - 1] == 'function') { + argsToAdd = args.slice(0, args.length - 1); + } else { + argsToAdd = args; + } debug.apply(_this, - [_this.name, i].concat(args.slice(0, args.length - 1))); + [_this.name, i].concat(argsToAdd)); } else if (debug instanceof stream.Writable) { this.$printToStream(_this.name, i, args, debug); } else { diff --git a/test/docs/debug.test.js b/test/docs/debug.test.js index db3e4b7523b..3cb82fab4ec 100644 --- a/test/docs/debug.test.js +++ b/test/docs/debug.test.js @@ -105,7 +105,7 @@ describe('debug: shell', function() { await Test.create({ name: 'foo' }); assert.equal(args.length, 1); assert.equal(args[0][1], 'insertOne'); - assert.strictEqual(args[0][3], undefined); + assert.strictEqual(args[0][4], undefined); await m.disconnect(); }); diff --git a/test/index.test.js b/test/index.test.js index 6e4aae99926..0976aab3479 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -90,6 +90,19 @@ describe('mongoose module:', function() { await mongoose.disconnect(); }); + it('should collect the args correctly gh-13364', async function() { + const util = require('util'); + const mongoose = new Mongoose(); + const conn = await mongoose.connect(start.uri); + let actual = ''; + mongoose.set('debug', (collectionName, methodName, ...methodArgs) => { + actual = `${collectionName}.${methodName}(${util.inspect(methodArgs).slice(2, -2)})`; + }); + const user = conn.connection.collection('User'); + await user.findOne({ key: 'value' }); + assert.equal('User.findOne({ key: \'value\' })', actual); + }); + it('{g,s}etting options', function() { const mongoose = new Mongoose(); diff --git a/test/query.test.js b/test/query.test.js index 4f991ff25af..760af5abce8 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -3984,7 +3984,7 @@ describe('Query', function() { let lastOptions = {}; m.set('debug', function(_coll, _method, ...args) { - lastOptions = args[args.length - 1]; + lastOptions = args[args.length - 2]; }); const connDebug = m.createConnection(start.uri);