-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Unable to filter response from 'find / findOne' within plugin #11426
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
Comments
Can you provide an mvce please? |
EDIT: This code has some typos in it, check out the code two comments below for an example that runs. import { model, Mongoose } from 'mongoose'
const ExampleSchema = newSchema({
hidden: Boolean
})
async function shouldRemove(doc) {
return doc.hidden
}
ExampleSchema.plugin((schema) => {
schema.post('findOne', async function(doc) {
if (await shouldRemove(doc)) return undefined
return doc
})
})
const ExampleModel = model('Example', ExampleSchema)
Mongoose.connect('<SOME_MONGOOSE_INSTANCE')
async function main() {
await ExampleModel.create({ hidden: true })
const empty = await ExampleModel.findOne({ hidden: true })
// empty = undefined
await ExampleModel.create({ hidden: false })
const found = await ExampleModel.findOne({ hidden: false })
// found = Doc<{ hidden: false }>
}
main() |
The example is a little contrived / simplified. Here we could add |
Hey @IslandRhythms, you added 'confirmed-bug', is this meant to be an existing feature? Also, here's some functional reproduction code: import mongoose, { model, Schema } from 'mongoose'
const ExampleSchema = new Schema({
hidden: Boolean
})
async function shouldRemove(doc) {
return doc.hidden
}
ExampleSchema.plugin((schema) => {
schema.post('findOne', async function(doc) {
if (await shouldRemove(doc)) return undefined
return doc
})
})
const ExampleModel = model('Example', ExampleSchema)
mongoose.connect('mongodb://localhost:27017/bailo')
async function main() {
await ExampleModel.create({ hidden: true })
const empty = await ExampleModel.findOne({ hidden: true })
console.log('empty', empty)
// empty = undefined
await ExampleModel.create({ hidden: false })
const found = await ExampleModel.findOne({ hidden: false })
console.log('found', found)
// found = Doc<{ hidden: false }>
}
main() The 'empty' log should be empty, but it isn't! |
Do you not see this issue as happening @vkarpov15? The latest script should clearly show a lack of ability to remove a model from the results of |
It is not a bug but a feature request. |
Ah, fair fair. Would you accept outside contributions? Could you point out what would need to be done to implement this? |
Sure. I am not an official maintainer and I kind of spam the project with PRs :). I dont know where precisely it needs to be patched. But the hooks are managed by the npm package named "kareem". So I would look into the files where kareem is required. |
I'm sorry it took me so long to get to this. We'll think more about how best to support this, but we'll have to add support in a minor release. First, worth noting: you can modify the result of ExampleSchema.plugin((schema) => {
schema.post('find', async function(docs) {
for (let i = docs.length - 1; i >= 0; --i) {
if (await shouldRemove(docs[i])) {
docs.splice(i, 1);
}
}
})
}) A few notes about couple of issues to work around for implementing this:
ExampleSchema.plugin((schema) => {
schema.post('findOne', async function(doc) {
if (await shouldRemove(doc)) return null
return doc
})
})
|
…ing the wrapped function's result Re: Automattic/mongoose#11426 Re: Automattic/mongoose#8393
feat: add `skipMiddlewareFunction()` and `overwriteMiddlewareResult()` for skipping and modifying middleware results
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Unable to filter response from a
.find()
or.findOne()
call.If the current behavior is a bug, please provide the steps to reproduce.
Given a plugin:
I should be able to selectively return either nothing, or a document, from
.find()
and.findOne()
calls.What is the expected behavior?
If
shouldRemove
returns true, for no document to be returned fromundefined
.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose v6.1.6, Node v16.13.2,
The text was updated successfully, but these errors were encountered: