Add orFail method

This commit is contained in:
Daniel Kesselberg
2018-12-06 17:20:25 +01:00
parent a7e0763b93
commit 07c66081db
2 changed files with 25 additions and 0 deletions
+24
View File
@@ -2021,6 +2021,30 @@ declare module "mongoose" {
*/
or(array: any[]): this;
/**
* Make this query throw an error if no documents match the given `filter`.
* This is handy for integrating with async/await, because `orFail()` saves you
* an extra `if` statement to check if no document was found.
*
* ####Example:
*
* // Throws if no doc returned
* await Model.findOne({ foo: 'bar' }).orFail();
*
* // Throws if no document was updated
* await Model.updateOne({ foo: 'bar' }, { name: 'test' }).orFail();
*
* // Throws "No docs found!" error if no docs match `{ foo: 'bar' }`
* await Model.find({ foo: 'bar' }).orFail(new Error('No docs found!'));
*
* // Throws "Not found" error if no document was found
* await Model.findOneAndUpdate({ foo: 'bar' }, { name: 'test' }).
* orFail(() => Error('Not found'));
*
* @param err optional error to throw if no docs match `filter`
*/
orFail(err?: Error | (() => Error)): this;
/** Specifies a $polygon condition */
polygon(...coordinatePairs: number[][]): this;
polygon(path: string, ...coordinatePairs: number[][]): this;
+1
View File
@@ -1080,6 +1080,7 @@ query.find().where('age').in([20, 21]);
query.find().in('age', [20, 21]);
query.nor([{ color: 'green' }, { status: 'ok' }]).nor([]);
query.or([{ color: 'red' }, { status: 'emergency' }]).or([]);
query.find({ color: 'blue' }).orFail();
query.where('loc').within().polygon([10,20], [13, 25], [7,15]);
query.polygon('loc', [10,20], [13, 25], [7,15]);
query.findOne().populate('owner').exec(function (err, kitten) {