We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
8.1.1
20.11.0
6.0.12
No response
If I define a virtual after model declaration, the virtual only works callingtoJSON() to document.
toJSON()
For now I've to use applyGetters() to the virtualType to get the value.
applyGetters()
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));
If doc.toJSON() works with a virtual defined after model, I should be able to get the virtual directly.
doc.toJSON()
In the docs the only restriction is to avoid hook definitions after model.
The text was updated successfully, but these errors were encountered:
feat(model): add recompileSchema() function to models to allow appl…
recompileSchema()
c480bd6
…ying schema changes after compiling Fix #14296
That's awesome, this allows me to develop all the other plugins that I couldn't because of this. problem.
Thks.
Sorry, something went wrong.
Merge branch 'vkarpov15/gh-14296' of github.com:Automattic/mongoose i…
d30f557
…nto vkarpov15/gh-14296
Merge pull request #14306 from Automattic/vkarpov15/gh-14296
f696543
feat(model): add `recompileSchema()` function to models to allow applying schema changes after compiling
We'll release 8.2 in the next few days with the new feature.
Successfully merging a pull request may close this issue.
Prerequisites
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 calling
toJSON()
to document.For now I've to use
applyGetters()
to the virtualType to get the value.Steps to Reproduce
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.
The text was updated successfully, but these errors were encountered: