You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Schema.clone() drops $timestamps, so embedded discriminators with a timestamped child schema throw on save #16650
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
9.11.4
Node.js version
24
MongoDB server version
8
Typescript version (if applicable)
No response
Description
If a schema contains a child schema with timestamps: true, Mongoose adds $timestamps and a timestampsPreSave hook to the parent. clone() copies the hook but not $timestamps, so saving a document built from the clone throws:
TypeError: Cannot destructure property 'createdAt' of 'schema.$timestamps' as it is undefined.
at EmbeddedDocument.timestampsPreSave (lib/helpers/timestamps/setupTimestamps.js:96:11)
Steps to Reproduce
const child = new Schema({ name: String }, { timestamps: true });
const base = new Schema({ kind: String }, { discriminatorKey: 'kind' });
const schema = new Schema({ items: [base] });
schema.path('items').discriminator('WithChild', new Schema({ child }));
const Test = mongoose.model('Test', schema);
await Test.create({ items: [{ kind: 'WithChild', child: { name: 'foo' } }] }); // throws
Activity
test: reference Automatticgh-16650