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

Throw error when transform returns a promise #9163

Closed
botv opened this issue Jun 22, 2020 · 1 comment · Fixed by #9176
Closed

Throw error when transform returns a promise #9163

botv opened this issue Jun 22, 2020 · 1 comment · Fixed by #9176
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

Comments

@botv
Copy link
Contributor

botv commented Jun 22, 2020

Am I doing something wrong defining a transformer on a schema? Here's my schema code:

const bookSchema = new Schema({
  title: {
    type: String,
    transform: async function(v) {
      return await Promise.resolve(v)
    }
  },
});

When I create a new Book, I get title: {}. Do transformers have to be synchronous?

@vkarpov15
Copy link
Collaborator

Yes they do. JSON.stringify() is a synchronous function, so transform needs to be synchronous as well.

@vkarpov15 vkarpov15 changed the title Asynchronous transform not working Throw error when transform returns a promise Jun 25, 2020
@vkarpov15 vkarpov15 added this to the 5.9.21 milestone Jun 25, 2020
@vkarpov15 vkarpov15 added the developer-experience This issue improves error messages, debugging, or reporting label Jun 25, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Jun 25, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Jun 25, 2020
vkarpov15 added a commit to AbdelrahmanHafez/mongoose that referenced this issue Jul 1, 2020
vkarpov15 added a commit that referenced this issue Jul 1, 2020
fix(document): disallow `transform` functions that return promises
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants