Query.prototype.orFail()
Parameters
- [err] «Function|Error» optional error to throw if no docs match
filter
. If not specified,orFail()
will throw aDocumentNotFoundError
Returns:
- «Query» 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'));
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .