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

Populate does not work when sanitizeFilter is true #10740

Closed
thiagokisaki opened this issue Sep 16, 2021 · 0 comments
Closed

Populate does not work when sanitizeFilter is true #10740

thiagokisaki opened this issue Sep 16, 2021 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@thiagokisaki
Copy link
Contributor

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?
Populate does not work when sanitizeFilter is true.

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

const mongoose = require('mongoose');
const { Schema, model } = mongoose;

mongoose.set('sanitizeFilter', true);

const userSchema = new Schema({
  name: String,
});

const User = model('User', userSchema);

const postSchema = new Schema({
  author: {
    type: Schema.Types.ObjectId,
    ref: 'User',
  },
});

const Post = model('Post', postSchema);

(async () => {
  await mongoose.connect(
    'mongodb://localhost:27017/sanitize-filter-populate-error'
  );

  const user = await User.create({ name: 'John Doe' });
  const post = await Post.create({ author: user._id });

  try {
    await post.populate('author');
  } catch (error) {
    console.error(error);
  }

  console.log(post);
})();

Error:

CastError: Cast to ObjectId failed for value "{ '$in': [ new ObjectId("614399760c0c857487f30596") ] }" (type Object) at path "_id" for model "User"

What is the expected behavior?
No error when populating.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: v14.17.3
Mongoose: v6.0.6
MongoDB: v5.0.2

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 16, 2021
@vkarpov15 vkarpov15 added this to the 6.0.7 milestone Sep 18, 2021
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