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

A population of virtual type in the nested path is not visible in toJSON. #13189

Closed
1 task done
csy1204 opened this issue Mar 20, 2023 · 1 comment · Fixed by #13197
Closed
1 task done

A population of virtual type in the nested path is not visible in toJSON. #13189

csy1204 opened this issue Mar 20, 2023 · 1 comment · Fixed by #13197
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@csy1204
Copy link
Contributor

csy1204 commented Mar 20, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.0.2

Node.js version

v16.13.2

MongoDB version

4.4

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.3

Issue

I have an issue about toJSON in a nested virtual path.
As shown in the code below, I have populated the virtual type of the path in the multi-level and I see that the data of c1, c2 exists. But when I do the toJSON, I don't see that data.
{ toJSON: { virtuals: true } option also didn't work. I would like your help if you can solve this problem.

Example Code

const mongoose = require('mongoose');

const { Schema } = mongoose;

const TagsSchema = new Schema({
  codes: [String]
}, { toJSON: { virtuals: true }, toObject: { virtuals: true } });

const PostSchema = new Schema({
  name: String,
  tag: TagsSchema
}, { toJSON: { virtuals: true }, toObject: { virtuals: true } });

PostSchema.virtual('tag.cs', {
  ref: 'Test2',
  localField: 'tag.codes',
  foreignField: 'code'
});

const TagSchema = new Schema({
  code: String,
  url: String
});

const Post = db.model('Test1', PostSchema);
const Tag = db.model('Test2', TagSchema);

const c1 = await Tag.create({ code: 'c1', url: 'https://www.google.com' });
const c2 = await Tag.create({ code: 'c2', url: 'https://www.google.com' });
const c3 = await Tag.create({ code: 'c3', url: 'https://www.google.com' });

const b1 = await Post.create({ name: 'b1', tag: { codes: ['c1', 'c2'] } });
const b2 = await Post.create({ name: 'b2', tag: { codes: ['c2'] } });
const b3 = await Post.create({ name: 'b3', tag: { codes: ['c3'] } });

// Pouplate the nested virtual path
const Bs = await Post.find().populate('tag.cs').exec();

console.log(Bs[0].tag.cs); // [c1, c2] is printed
// [
//   {
//     _id: '641823432714c10f648c056b',
//     code: 'c1',
//     url: 'https://www.google.com',
//     __v: 0
//   },
//   {
//     _id: '641823452714c10f648c056d',
//     code: 'c2',
//     url: 'https://www.google.com',
//     __v: 0
//   }
// ]
console.log(Bs[0].toJSON().tag.cs); // undefined -> c1, c2 is expected
console.log(Bs[0].tag.toJSON().cs); // undefined -> c1, c2 is expected
@csy1204 csy1204 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 20, 2023
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 21, 2023
@vkarpov15 vkarpov15 added this to the 7.0.3 milestone Mar 21, 2023
@csy1204
Copy link
Contributor Author

csy1204 commented Mar 21, 2023

mongoose/lib/virtualtype.js

Lines 140 to 145 in f490627

VirtualType.prototype.applyGetters = function(value, doc) {
if (utils.hasUserDefinedProperty(this.options, ['ref', 'refPath']) &&
doc.$$populatedVirtuals &&
doc.$$populatedVirtuals.hasOwnProperty(this.path)) {
value = doc.$$populatedVirtuals[this.path];
}

I think the problem is that doc.$$populatedVirtuals is undefined - for whatever reason, $$populatedVirtuals was not created.

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 22, 2023
vkarpov15 added a commit that referenced this issue Mar 22, 2023
…quivalent to creating virtual on the subdocument

Fix #13189
Re: #8210
Re: #8198
vkarpov15 added a commit that referenced this issue Mar 22, 2023
vkarpov15 added a commit that referenced this issue Mar 23, 2023
fix(schema): make creating top-level virtual underneath subdocument equivalent to creating virtual on the subdocument
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
2 participants