0

I want to apply throwing errors for all "one" operations where the document is missing.

I'm following the advice from one of mongoose collaborators:

mongoose.plugin((schema) => {
  schema.post(/One/, function (doc, next) {
    if (doc) return next()
    
    
    const query = this.getFilter() // doesn't work as expected
    const model = this.getFilter().collectionName
    const msg = `${model} for query ${JSON.stringify(query)} not found`
    return next(new MissingDocumentError(msg))
  })
})

The problem is, getFilter() returns the collection id and name, but not the query itself as it appears in the docs.

This is what I'm actually running

Tool.findOne({ key: "my-key" })

How do I get the actual query inside the query post middleware?

Shining Love Star
  • 5,734
  • 5
  • 39
  • 49

0 Answers0