https://github.com/alorel/mongoose-find-or-throw-plugin
Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.
https://github.com/alorel/mongoose-find-or-throw-plugin
check find found if mongo mongodb mongoose not null nullcheck or plugin throw typed types
Last synced: 4 months ago
JSON representation
Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.
- Host: GitHub
- URL: https://github.com/alorel/mongoose-find-or-throw-plugin
- Owner: Alorel
- License: mit
- Created: 2018-02-13T21:59:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-07T21:50:42.000Z (almost 7 years ago)
- Last Synced: 2024-04-14T00:17:48.363Z (over 1 year ago)
- Topics: check, find, found, if, mongo, mongodb, mongoose, not, null, nullcheck, or, plugin, throw, typed, types
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-throw-if-not-found-plugin
[](https://www.npmjs.com/package/mongoose-throw-if-not-found-plugin)
[](https://travis-ci.org/Alorel/mongoose-find-or-throw-plugin)
[](https://coveralls.io/github/Alorel/mongoose-find-or-throw-plugin?branch=master)
[](https://greenkeeper.io/)
Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.
Before:
```js
MyModel.findOne({foo: 'bar'})
.then(doc => {
if (!doc) {
throw new Error('Doc not found');
}
useDoc(doc);
});
```After:
```js
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:
```js
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:```typescript
import 'mongoose-throw-if-not-found-plugin';
```