-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
why transform.options auto convert inline params model to json ? #11776
Comments
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
const ASchema = new Schema({
name: String
})
const BSchema = new Schema({
name: String,
aName: String
})
ASchema.set('toObject', {
transform: (doc, ret, options) => {
console.log('correct:', options.b.aName) // now this is the correct way
console.log('error:', options.b.get('aName')) // in mongoose 4 we use this.
}
})
const AModel = mongoose.model('AModel', ASchema)
const BModel = mongoose.model('BModel', BSchema)
let main = async () => {
await mongoose.connect('mongodb://localhost/test_mongoose');
await mongoose.connection.dropDatabase();
const a = new AModel()
a.name = 'a'
await a.save()
const b = new BModel()
b.name = 'b';
b.aName = 'a';
await b.save()
console.log(a.toObject({b})) // pass in a model, but no longer get a model
}
main().then(() => {console.log('done')}) |
i don't know if |
Count this as another use case for #11849. We'll use shallow clone because there's no reason for us to deep clone |
thanks! |
Do you want to request a feature or report a bug?
i'm not sure it is a bug or feature, but the behaviors change since mongoose 4 -> 5/6. and without any breaking changelogs.
What is the current behavior?
for now, if you define the
transform
function for a schema, the inline params passed intooptions
no longer support mongoose model. (but supports in mongoose 4). so you could not calloptions.model.get
anymore.If the current behavior is a bug, please provide the steps to reproduce.
here is a gist to reproduce the behaviors .
https://gist.github.com/smallst/506f493bbe47f5bf1d62d1485b88d12c
see line 34 and 14-17.
and here is the running results
What is the expected behavior?
i think if we pass in a
model
, it should be still amodel
inside transform function. Or you could tell me (in the Doc) why it is not.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
version:
nodejs: v17.9.0
mongoose: 6.3.2
mongodb: 4.0
The text was updated successfully, but these errors were encountered: