Skip to content

Commit

Permalink
fix: ensure log pre-save and pre-validate hooks fire
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Feb 27, 2025
1 parent bc1ba56 commit bf42d0f
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions helpers/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async function hook(err, message, meta) {
if (!conn.models || !conn.models.Logs || !conn.models.Logs.create)
throw new Error('Mongoose logs model not yet initialized');

const log = { err, message, meta };
const log = new conn.models.Logs({ err, message, meta });

if (typeof log.meta === 'object') {
// user
Expand Down Expand Up @@ -228,9 +228,8 @@ async function hook(err, message, meta) {
// this should never happen but it's a conditional safeguard
if (_.isError(log.err)) log.err = JSON.parse(safeStringify(log.err));

log.hash = conn.models.Logs.getQueryHash(log);

return conn.models.Logs.create(log)
return log
.save() // log.save() ensures pre-validate and pre-save hooks fire
.then()
.catch((err) => {
// return early if it was a duplicate log being created
Expand Down

0 comments on commit bf42d0f

Please sign in to comment.