diff --git a/lib/model.js b/lib/model.js index 783c0dd40b..6410e6ff8b 100644 --- a/lib/model.js +++ b/lib/model.js @@ -4627,9 +4627,10 @@ function _execPopulateQuery(mod, match, select, assignmentOpts, callback) { for (const pop of subPopulate) { pop._fullPath = basePath + '.' + pop.path; } - } else if (subPopulate != null) { + } else if (subPopulate != null && typeof subPopulate === 'object') { subPopulate._fullPath = basePath + '.' + subPopulate.path; } + query.populate(subPopulate); } diff --git a/test/document.populate.test.js b/test/document.populate.test.js index 2709e47288..52c76f432d 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -968,6 +968,40 @@ describe('document.populate', function() { assert.ok(finalDoc.populated('i.g')); assert.ok(finalDoc.i.populated('g')); + }); + + it('works with single strings (gh-11160)', async() => { + const bookSchema = mongoose.Schema({ + title: String, + authorId: { type: Schema.ObjectId, ref: 'Author' } + }); + + const authorSchema = mongoose.Schema({ + name: String, + websiteId: { type: Schema.ObjectId, ref: 'Website' } + }); + + const websiteSchema = mongoose.Schema({ + url: String + }); + + const Book = db.model('Book', bookSchema); + const Author = db.model('Author', authorSchema); + const Website = db.model('Website', websiteSchema); + + + const website = new Website({ url: 'http://www.clean-code.com' }); + const author = new Author({ name: 'Robert C. Martin', websiteId: website._id }); + const book = new Book({ title: 'Clean Code', authorId: author._id }); + await Promise.all([ + website.save(), + author.save(), + book.save() + ]); + const foundBook = await Book.findOne({ _id: book._id }); + await foundBook.populate({ path: 'authorId', populate: 'websiteId' }); + assert.ok(foundBook.populated('authorId')); + assert.ok(foundBook.authorId.populated('websiteId')); }); }); diff --git a/test/typescript/main.test.js b/test/typescript/main.test.js index 2729ef5a0b..e78d9e9d05 100644 --- a/test/typescript/main.test.js +++ b/test/typescript/main.test.js @@ -4,44 +4,6 @@ const assert = require('assert'); const typescript = require('typescript'); const tsconfig = require('./tsconfig.json'); -function printTSErrors(errors) { - if (!process.env.D) { - return; - } - if (!errors.length) { - return; - } - errors.forEach(e => { - if (typeof e.messageText === 'string') { - let lineStart = e.file.text.slice(0, e.start).lastIndexOf('\n'); - if (lineStart === -1) { - lineStart = 0; - } - let lineEnd = e.file.text.slice(e.start).indexOf('\n'); - if (lineEnd === -1) { - lineEnd = e.file.text.length; - } else { - lineEnd += e.start; - } - console.log(`-----\n\nERROR: ${e.messageText}\n\n${e.file.text.slice(lineStart, lineEnd - 1)}\n${' '.repeat(e.start - lineStart - 1)}^`); - } else if (e.messageText.messageText) { - let lineStart = e.file.text.slice(0, e.start).lastIndexOf('\n'); - if (lineStart === -1) { - lineStart = 0; - } - let lineEnd = e.file.text.slice(e.start).indexOf('\n'); - if (lineEnd === -1) { - lineEnd = e.file.text.length; - } else { - lineEnd += e.start; - } - console.log(`-----\n\nERROR: ${e.messageText.messageText}\n\n${e.file.text.slice(lineStart, lineEnd - 1)}\n${' '.repeat(e.start - lineStart - 1)}^`); - } else { - console.log(e); - } - }); -} - describe('typescript syntax', function() { this.timeout(60000); @@ -248,4 +210,42 @@ function runTest(file, configOverride) { const emitResult = program.emit(); return typescript.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); +} + +function printTSErrors(errors) { + if (!process.env.D) { + return; + } + if (!errors.length) { + return; + } + errors.forEach(e => { + if (typeof e.messageText === 'string') { + let lineStart = e.file.text.slice(0, e.start).lastIndexOf('\n'); + if (lineStart === -1) { + lineStart = 0; + } + let lineEnd = e.file.text.slice(e.start).indexOf('\n'); + if (lineEnd === -1) { + lineEnd = e.file.text.length; + } else { + lineEnd += e.start; + } + console.log(`-----\n\nERROR: ${e.messageText}\n\n${e.file.text.slice(lineStart, lineEnd - 1)}\n${' '.repeat(e.start - lineStart - 1)}^`); + } else if (e.messageText.messageText) { + let lineStart = e.file.text.slice(0, e.start).lastIndexOf('\n'); + if (lineStart === -1) { + lineStart = 0; + } + let lineEnd = e.file.text.slice(e.start).indexOf('\n'); + if (lineEnd === -1) { + lineEnd = e.file.text.length; + } else { + lineEnd += e.start; + } + console.log(`-----\n\nERROR: ${e.messageText.messageText}\n\n${e.file.text.slice(lineStart, lineEnd - 1)}\n${' '.repeat(e.start - lineStart - 1)}^`); + } else { + console.log(e); + } + }); } \ No newline at end of file