From 11ace49d8c1e75a41200bb45c0bd50c712d2e5d2 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:42:07 +0200 Subject: [PATCH] :bug: fix findOne exists query --- src/models/index.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/models/index.ts b/src/models/index.ts index 61c8d85a..11a6fe37 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -52,10 +52,17 @@ mongoose.plugin((schema: Schema, opts: any) => { }); }, }); - schema.post("findOne", (doc, next) => { - if (!doc) return next(new HTTPError("Not found", 404)); - // @ts-ignore - return next(); + schema.post("findOne", function (doc, next) { + try { + // @ts-ignore + const isExistsQuery = JSON.stringify(this._userProvidedFields) === JSON.stringify({ _id: 1 }); + if (!doc && !isExistsQuery) return next(new HTTPError("Not found", 404)); + // @ts-ignore + return next(); + } catch (error) { + // @ts-ignore + next(); + } }); });