Added error checking and fixed typo

This commit is contained in:
Alex Holliday committed 2024-07-03 12:32:42 -07:00
1 parent 031ae06d41
commit 505d9f2890
1 file changed
+7 -3
+7 -3
View File
@@ -37,8 +37,9 @@ const CheckSchema = mongoose.Schema(
*/ */
CheckSchema.pre("save", async function (next) { CheckSchema.pre("save", async function (next) {
try {
const monitor = await mongoose.model("Monitor").findById(this.monitorId); const monitor = await mongoose.model("Monitor").findById(this.monitorId);
if (monitor.status !== this.status) { if (monitor && monitor.status !== this.status) {
if (monitor.status === true && this.status === false) { if (monitor.status === true && this.status === false) {
// TODO issue alert // TODO issue alert
console.log("Monitor went down"); console.log("Monitor went down");
@@ -50,8 +51,11 @@ CheckSchema.pre("save", async function (next) {
} }
await monitor.save(); await monitor.save();
} }
} catch (error) {
next; console.log(error);
} finally {
next();
}
}); });
module.exports = mongoose.model("Check", CheckSchema); module.exports = mongoose.model("Check", CheckSchema);