properly handles not found by id
This commit is contained in:
@@ -11,6 +11,8 @@ const find = async (options) => {
|
||||
depth,
|
||||
} = options;
|
||||
|
||||
// await pre find hook here
|
||||
|
||||
const mongooseQuery = await Model.buildQuery(query, locale);
|
||||
|
||||
const paginateQuery = {
|
||||
@@ -26,6 +28,8 @@ const find = async (options) => {
|
||||
|
||||
const result = await Model.paginate(mongooseQuery, paginateQuery);
|
||||
|
||||
// await post find hook here
|
||||
|
||||
return {
|
||||
...result,
|
||||
docs: result.docs.map((doc) => {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
const { NotFound } = require('../../errors');
|
||||
|
||||
const findByID = async (options) => {
|
||||
const mongooseOptions = {};
|
||||
const { depth } = options;
|
||||
@@ -12,6 +14,11 @@ const findByID = async (options) => {
|
||||
// Await pre findOne hook here
|
||||
|
||||
const doc = await options.Model.findOne({ _id: options.id }, {}, mongooseOptions);
|
||||
|
||||
if (!doc) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
if (options.locale && doc.setLocale) {
|
||||
doc.setLocale(options.locale, options.fallback);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user