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

[Bug] populate with clone: true throws an error with lean #8760

Closed
AbdelrahmanHafez opened this issue Apr 2, 2020 · 0 comments
Closed

[Bug] populate with clone: true throws an error with lean #8760

AbdelrahmanHafez opened this issue Apr 2, 2020 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented Apr 2, 2020

What is the current behavior?
Can not use clone: true in populate along with lean(), it throws an error:

TypeError: doc.constructor.hydrate is not a function
    at assignRawDocsToIdStructure (node_modules\mongoose\lib\helpers\populate\assignRawDocsToIdStructure.js:58:29)
    at assignRawDocsToIdStructure (node_modules\mongoose\lib\helpers\populate\assignRawDocsToIdStructure.js:41:7)
    at assignVals (node_modules\mongoose\lib\helpers\populate\assignVals.js:26:3)
    at _assign (node_modules\mongoose\lib\model.js:4564:3)
    at _done (node_modules\mongoose\lib\model.js:4411:7)
    at _next (node_modules\mongoose\lib\model.js:4403:7)
    at node_modules\mongoose\lib\model.js:4837:16
    at node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
    at node_modules\mongoose\lib\model.js:4860:21
    at node_modules\mongoose\lib\query.js:4366:11
    at node_modules\kareem\index.js:135:16
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Function instance at:
    at node_modules\mongoose\lib\model.js:4839:13
    at node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:79:11)

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');

mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true });

const blogPostSchema = new Schema({
  title: { type: String },
  commentsIds: [{ type: Schema.ObjectId, ref: 'Comment' }]
});

const commentSchema = new Schema({
  content: String
});

const BlogPost = mongoose.model('BlogPost', blogPostSchema);
const Comment = mongoose.model('Comment', commentSchema);

async function run () {
  await BlogPost.deleteMany();

  const blogPost = new BlogPost({ title: 'Important post' });
  const comment = new Comment({ content: 'Cool post.' });

  blogPost.commentsIds = [comment._id];

  await Promise.all([
    blogPost.save(),
    comment.save()
  ]);

  const foundBlogPost = await BlogPost.findOne({ _id: blogPost._id }).populate({
    path: 'commentsIds',
    options: { clone: true }
  }).lean();

  assert.equal(foundBlogPost.commentsIds[0].content, 'Cool post.');

  console.log('Done.');
}


run().catch(console.error);

What is the expected behavior?

To populate successfully.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

mongoose v5.9.7
MongoDB v4.0.4
Node v12.0

Will be putting a PR to solve this sometime soon.

AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Apr 4, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Apr 4, 2020
@vkarpov15 vkarpov15 added this to the 5.9.9 milestone Apr 11, 2020
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 11, 2020
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

Successfully merging a pull request may close this issue.

2 participants