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

Can not access document from virtual populated array. #8198

Closed
quanhv94 opened this issue Sep 26, 2019 · 0 comments
Closed

Can not access document from virtual populated array. #8198

quanhv94 opened this issue Sep 26, 2019 · 0 comments
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@quanhv94
Copy link

quanhv94 commented Sep 26, 2019

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Can not access document from virtual populated array.
If the current behavior is a bug, please provide the steps to reproduce.

const FooSchema = new Schema({
  name: String,
  children: [{
    barId: { type: Schema.Types.ObjectId, ref: 'Bar' },
    quantity: Number,
  }],
}, {
  toJSON: { virtuals: true },
  toObject: { virtuals: true },
});
FooSchema.virtual('children.bar', {
  ref: 'Bar',
  localField: 'children.barId',
  foreignField: '_id',
  justOne: true,
});

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

const Foo = mongoose.model('Foo', FooSchema);
const Bar = mongoose.model('Bar', BarSchema);
(async () => {
  const bar = await Bar.create({ name: 'bar' });
  const foo = await Foo.create({ name: 'foo', children: [{ barId: bar._id, quantity: 1 }] });
  const foo2 = await Foo.findById(foo._id).populate('children.bar');
  console.log(foo2.children);
  // [
  //   {
  //     _id: 5d8c395c70fda60a1e83e433,
  //     barId: 5d8c395c70fda60a1e83e431,
  //     quantity: 1,
  //     bar: { _id: 5d8c395c70fda60a1e83e431, name: 'bar', __v: 0 }
  //   }
  // ]
  console.log(foo2.children[0].bar); // this get undefined
  // undefined
  console.log(foo2.toJSON().children[0].bar); // this actually work, but just a json object
  // { _id: 5d8c395c70fda60a1e83e431, name: 'bar', __v: 0 }
})();

What is the expected behavior?
Can access document from virtual populated array.
What are the versions of Node.js, Mongoose and MongoDB you are using?
Nodejs: 12.9.1
Mongoose: 5.7.1
MongoDB: 4.0.10

Please correct me if I have a mistake.

@vkarpov15 vkarpov15 modified the milestones: 5.8.0, 5.7.4 Sep 30, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Sep 30, 2019
vkarpov15 added a commit that referenced this issue Oct 3, 2019
vkarpov15 added a commit that referenced this issue Mar 22, 2023
…quivalent to creating virtual on the subdocument

Fix #13189
Re: #8210
Re: #8198
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants