New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR #12254 introduced bug: Methods defined in shared schemas get removed #12423
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
maximilianschmid
changed the title
PR #12254 introduced major bug: Methods defined in shared schemas get removed
PR #12254 introduced bug: Methods defined in shared schemas get removed
Sep 12, 2022
IslandRhythms
added
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
labels
Sep 12, 2022
const mongoose = require('mongoose')
mongoose.set('debug', true)
const sharedSubSchema = new mongoose.Schema({
name: {
type: String
}
})
sharedSubSchema.methods.sharedSubSchemaMethod = function () {
console.log('sharedSubSchemaMethod called')
}
const mainDocumentSchema = new mongoose.Schema({
subdocuments: {
type: [sharedSubSchema],
required: true
}
})
const MainDocument = mongoose.model('MainDocument', mainDocumentSchema)
const secondaryDocumentSchema = new mongoose.Schema({
subdocuments: {
type: [sharedSubSchema],
required: true
}
})
const SecondaryDocument = mongoose.model('SecondaryDocument', secondaryDocumentSchema)
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const mainDoc = await MainDocument.create({
subdocuments: [
{
name: 'one'
}
]
})
const secondaryDoc = await SecondaryDocument.create({
subdocuments: [
{
name: 'secondary'
}
]
})
console.log(mainDoc.subdocuments[0].sharedSubSchemaMethod)
console.log(secondaryDoc.subdocuments[0].sharedSubSchemaMethod)
}
run(); |
👍 |
Just to clarify: version 6.6.0 also has this bug. |
Just stumbled upon the same problem. For reference: The corresponding PR is #12391 |
vkarpov15
added a commit
that referenced
this issue
Sep 21, 2022
Uzlopak
pushed a commit
that referenced
this issue
Sep 21, 2022
Thx! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
6.5.5
Node.js version
16.6
MongoDB server version
6.0.1
Description
PR #12254 introduces a bug
where instance methods defined in shared schema get deleted.
Problem located in line 38 in file
mongoose/lib/helpers/model/applyMethods.js
Commit
9fbf92c#diff-9de3851c9d06234f6bef122196730d35ce2dc68fc2b609824b1676194608e4fb
Steps to Reproduce
Expected Behavior
Sub-Document method should be defined on both models, not only one.
Buggy output mongoosejs 6.5.5
[Function (anonymous)]
undefined
Correct output mongoosejs 6.5.4
[Function (anonymous)]
[Function (anonymous)]
The text was updated successfully, but these errors were encountered: