-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Description
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
We have a schema with (1) nested documents, which (2) use discriminators, and where (3) one of the discriminated types uses a refPath which is a function. Before updating Mongoose to 5.9.6, this setup has worked fine. Now, our server crashes b/c Mongoose tries to call split on a function in getModelsMapForPopulate.js:216:40.
If the current behavior is a bug, please provide the steps to reproduce.
import * as mongoose from 'mongoose';
beforeAll(async () => mongoose.connect(process.env.MONGO_URL));
afterAll(async () => mongoose.disconnect());
it('Mongoose refPath issue', async () => {
const nested = new mongoose.Schema(
{
// nothing here
},
{
discriminatorKey: 'type'
}
);
const nestedDiscriminated = new mongoose.Schema({
fooType: { type: String },
foo: {
type: mongoose.Schema.Types.ObjectId,
refPath: (doc: any, path: string) => path.replace('.foo', '.fooType')
}
});
const main = new mongoose.Schema({
items: [nested]
});
const itemsType = main.path('items') as mongoose.Schema.Types.DocumentArray;
itemsType.discriminator('discriminated', nestedDiscriminated);
const MainModel = mongoose.model<any>('refPathIssue_main', main);
const OtherModel = mongoose.model<any>('refPathIssue_other', new mongoose.Schema({
name: { type: String }
}));
const otherDoc = new OtherModel({ name: 'hello world' });
await otherDoc.save();
await new MainModel({
items: [{
type: 'discriminated',
fooType: 'refPathIssue_other',
foo: otherDoc._id
}]
}).save();
const result = await MainModel.find({}).populate('items.foo').exec();
expect(result[0].items[0].foo.name).toEqual('hello world');
});(fwiw, the test would pass if I move the Schema properties from nestedDiscriminated up to nested)
What is the expected behavior?
The test should pass, and the document should be populated. Instead we’re now receiving the following error:
TypeError: normalizedRefPath.split is not a function
at getModelsMapForPopulate (node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:216:40)
at populate (node_modules/mongoose/lib/model.js:4300:21)
at _populate (node_modules/mongoose/lib/model.js:4270:5)
at node_modules/mongoose/lib/model.js:4245:5
at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
at Function.Model.populate (node_modules/mongoose/lib/model.js:4243:10)
at cb (node_modules/mongoose/lib/query.js:1934:17)
at node_modules/mongodb/lib/utils.js:731:5
at handleCallback (node_modules/mongodb/lib/utils.js:128:55)
at node_modules/mongodb/lib/cursor.js:840:66
at handleCallback (node_modules/mongodb/lib/utils.js:128:55)
at completeClose (node_modules/mongodb/lib/cursor.js:934:16)
at Cursor.close (node_modules/mongodb/lib/cursor.js:953:12)
at node_modules/mongodb/lib/cursor.js:840:27
at handleCallback (node_modules/mongodb/lib/core/cursor.js:32:5)
at node_modules/mongodb/lib/core/cursor.js:694:38
Looking at the code comments, this seems to be a regression from this fix:
#8452
Downgrading to 5.8.3 solved the issue.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
- Node.js v13.8.0
- Mongoose 5.9.6
- MongoDB 3.6.7