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

Virtual declared after model creation, doesn't work #14296

Closed
2 tasks done
chumager opened this issue Jan 25, 2024 · 2 comments · Fixed by #14306
Closed
2 tasks done

Virtual declared after model creation, doesn't work #14296

chumager opened this issue Jan 25, 2024 · 2 comments · Fixed by #14306
Labels
priority Automatically set for Mongoose Pro subscribers
Milestone

Comments

@chumager
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.1.1

Node.js version

20.11.0

MongoDB server version

6.0.12

Typescript version (if applicable)

No response

Description

If I define a virtual after model declaration, the virtual only works callingtoJSON() to document.

For now I've to use applyGetters() to the virtualType to get the value.

Steps to Reproduce

import db from "mongoose";
//await db.connect("mongodb://127.0.0.1:27017/test");
const Schema = new db.Schema({
  field: String
});
Schema.virtual("virtual1").get(function () {
  return this.field + " virtual1";
});
Schema.virtual("virtual2").get(function () {
  return this.virtualAfter + " virtual2";
});
Schema.virtual("virtual3").get(function () {
  return this.constructor.schema.virtualpath("virtual4").applyGetters(undefined, this) + " virtual3";
});
const Model = db.model("model", Schema);
Model.schema.virtual("virtual4").get(function () {
  return this.field + " virtual4";
});

const doc = new Model({field: "test"});
console.log("doc", doc);
console.log("doc JSON", doc.toJSON({virtuals: true}));
console.log("doc.virtual1", doc.virtual1);
console.log("doc.virtual2", doc.virtual2);
console.log("doc.virtual3", doc.virtual3);
console.log("doc.virtual4", doc.virtual4);
console.log("doc.virtual1 applyGetters", Schema.virtualpath("virtual1").applyGetters(undefined, doc));
console.log("doc.virtual2 applyGetters", Schema.virtualpath("virtual2").applyGetters(undefined, doc));
console.log("doc.virtual3 applyGetters", Schema.virtualpath("virtual3").applyGetters(undefined, doc));
console.log("doc.virtual4 applyGetters", Schema.virtualpath("virtual4").applyGetters(undefined, doc));

Expected Behavior

If doc.toJSON() works with a virtual defined after model, I should be able to get the virtual directly.

In the docs the only restriction is to avoid hook definitions after model.

@mongoose-pro-bot mongoose-pro-bot added the priority Automatically set for Mongoose Pro subscribers label Jan 25, 2024
vkarpov15 added a commit that referenced this issue Jan 29, 2024
@vkarpov15 vkarpov15 added this to the 8.2 milestone Jan 31, 2024
@chumager
Copy link
Author

chumager commented Feb 1, 2024

That's awesome, this allows me to develop all the other plugins that I couldn't because of this. problem.

Thks.

vkarpov15 added a commit that referenced this issue Feb 6, 2024
feat(model): add `recompileSchema()` function to models to allow applying schema changes after compiling
@vkarpov15
Copy link
Collaborator

We'll release 8.2 in the next few days with the new feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority Automatically set for Mongoose Pro subscribers
Projects
None yet
3 participants