mongoose-throw-if-not-found-plugin
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

mongoose-throw-if-not-found-plugin

NPM link

Build Status Coverage Status Greenkeeper badge Supports Node >= 6

Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.

Before:

MyModel.findOne({foo: 'bar'})
  .then(doc => {
    if (!doc) {
      throw new Error('Doc not found');
    }
    
    useDoc(doc);
  });

After:

MyModel.findOne({foo: 'bar'})
  .throwIfEmpty()
  .then(doc => {
    useDoc(doc);
  })
  .catch(e => {
    console.log(e.status); // 404
    console.log(e.message); // Not found
    console.log(e.name); // MongooseDocumentNotFoundError
  })

Usage

Simply import the library once anywhere in your code:

require('mongoose-throw-if-not-found-plugin');

It'll handle itself from there ;)

TypeScript usage

While typings are provided, sometimes your IDE may fail to recognise them for code hints. If this happens, simply add the import to the file you're using it from:

import 'mongoose-throw-if-not-found-plugin';

Dependents (0)

Package Sidebar

Install

npm i mongoose-throw-if-not-found-plugin

Weekly Downloads

8

Version

0.1.0

License

MIT

Unpacked Size

5.28 kB

Total Files

5

Last publish

Collaborators

  • alorel