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
6.5.0
16.15.0
5.0
The clone function from a Query doesn't trigger the hooks defined in its schema.
It may leads to an error as it doesn't rexecute the exact same query.
const schema = new Schema().pre("find", () => console.log("pre hook function")) const myModel = model("Test", schema) const query = myModel.find() await query.clone() // Do nothing await query // Log "pre hook function"
The only workaround is a "deep copy":
const schema = new Schema().pre("find", () => console.log("pre hook function")) const myModel = model("Test", schema) const initialQuery = myModel.find() // Magic is here const queryCloned = initialQuery.model.find(initialQuery.getFilter()) await queryCloned() // Log "pre hook function"
Execute the schema hooks on a cloned Query.
The text was updated successfully, but these errors were encountered:
const mongoose = require('mongoose'); const { Schema } = mongoose; async function run() { await mongoose.connect('mongodb://localhost:27017'); await mongoose.connection.dropDatabase(); const schema = new Schema().pre("find", () => console.log("pre hook function")) const myModel = mongoose.model("Test", schema) const query = myModel.find() const result = await query.clone() // Do nothing const res = await query // Log "pre hook function" console.log(result); console.log(res); } run();
Sorry, something went wrong.
fix(query): use correct Query constructor when cloning query
19dd0a2
Fix #12365
Merge pull request #12418 from Automattic/vkarpov15/gh-12365
805c927
Successfully merging a pull request may close this issue.
Prerequisites
Mongoose version
6.5.0
Node.js version
16.15.0
MongoDB server version
5.0
Description
The clone function from a Query doesn't trigger the hooks defined in its schema.
It may leads to an error as it doesn't rexecute the exact same query.
Steps to Reproduce
The only workaround is a "deep copy":
Expected Behavior
Execute the schema hooks on a cloned Query.
The text was updated successfully, but these errors were encountered: