From 505d9f28907bd7af7b03c5f7f59640f0ddce0136 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 3 Jul 2024 12:32:42 -0700 Subject: [PATCH] Added error checking and fixed typo --- Server/models/Check.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Server/models/Check.js b/Server/models/Check.js index b1cda5616..2bcb912f4 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -37,21 +37,25 @@ const CheckSchema = mongoose.Schema( */ CheckSchema.pre("save", async function (next) { - const monitor = await mongoose.model("Monitor").findById(this.monitorId); - if (monitor.status !== this.status) { - if (monitor.status === true && this.status === false) { - // TODO issue alert - console.log("Monitor went down"); - } + try { + const monitor = await mongoose.model("Monitor").findById(this.monitorId); + if (monitor && monitor.status !== this.status) { + if (monitor.status === true && this.status === false) { + // TODO issue alert + console.log("Monitor went down"); + } - if (monitor.status === false && this.status === true) { - // TODO issue alert - console.log("Monitor went up"); + if (monitor.status === false && this.status === true) { + // TODO issue alert + console.log("Monitor went up"); + } + await monitor.save(); } - await monitor.save(); + } catch (error) { + console.log(error); + } finally { + next(); } - - next; }); module.exports = mongoose.model("Check", CheckSchema);