mongoose-find-or-error
Simple Mongoose plugin for rejecting findOne
and findById
promises which resolve null.
Installation
npm install mongoose-find-or-error
Why create this plugin?
I find myself many times wanting to end a promise chain if a document is not found in the database. Without this plugin or similar solution I would end up doing something similar to this:
User ; ;
Why not reduce the boilerplate by doing something like this:
User ; ;
How to use
Hook the plugin to a schema:
// example.js const mongoose = ;const findOrErrorPlugin = ; const schema = name: String; schema; mongoose;
Execute a query:
const Example = ; // use with findById Example ; // or use with findOne Example ;
You can override the default errors by defining plugin options:
schema;
Running tests
npm test