properly handles not found by id

This commit is contained in:
James
2020-04-05 17:30:46 -04:00
parent 2d02691a79
commit 39537da71b
2 changed files with 11 additions and 0 deletions

View File

@@ -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) => {

View File

@@ -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);
}