Skip to content
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

Closed
smallst opened this issue May 6, 2022 · 5 comments
Closed

why transform.options auto convert inline params model to json ? #11776

smallst opened this issue May 6, 2022 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@smallst
Copy link

smallst commented May 6, 2022

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 into options no longer support mongoose model. (but supports in mongoose 4). so you could not call options.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
image

What is the expected behavior?
i think if we pass in a model, it should be still a model 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

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 9, 2022
@IslandRhythms
Copy link
Collaborator

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')})

@smallst
Copy link
Author

smallst commented May 10, 2022

i think the bug comes from here and here, you return the ._doc when clone mongoose model and this clone called on transform options.

and in mongoose4 you don't clone such options so goes well

@smallst
Copy link
Author

smallst commented May 10, 2022

i don't know if utils.clone should work as clone mongoose model to normal object (._doc) , and if utils.clone returns mongoose model as model would cause any issue. but maybe another utils.cloneModel function can work here? which clone everything as everything.
not sure if this suggests helps.
and i'm still using mongoose 5 (just upgraded from mongoose 4) in my company, that's a huge codebase so maybe not so easy to upgrade to mongoose 6 soon, if you fix this bug on mongoose 6 could you also publish a new version of mongoose 5 with same bugfix? thanks a lot

@vkarpov15 vkarpov15 added this to the 6.3.6 milestone Jun 5, 2022
@vkarpov15
Copy link
Collaborator

Count this as another use case for #11849. We'll use shallow clone because there's no reason for us to deep clone options.

@smallst
Copy link
Author

smallst commented Jun 7, 2022

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants