From 878b87851051953a6b950fa416c87b50c3b29208 Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Wed, 20 Jan 2021 18:11:39 +0200 Subject: [PATCH 1/2] Catch error from websocket device status dedupe --- lib/server/websocket.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/server/websocket.js b/lib/server/websocket.js index 9ca7c200c69..3c1c55ff873 100644 --- a/lib/server/websocket.js +++ b/lib/server/websocket.js @@ -408,12 +408,18 @@ function init (env, ctx, server) { // try to find exact match ctx.store.collection(collection).find(queryDev).toArray(function findResult (err, array) { - if (err || array.length > 0) { - console.log(LOG_DEDUP + 'Devicestatus exact match'); - if (callback) { - callback([array[0]]); - } + if (err) { + console.error(err); + callback([]); return; + } else { + if (array.length > 0) { + console.log(LOG_DEDUP + 'Devicestatus exact match'); + if (callback) { + callback([array[0]]); + } + return; + } } }); ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) { From 6b4b4abb3b39faa2c8c9bcd1c9199a5fa03e5176 Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Thu, 21 Jan 2021 13:45:40 +0200 Subject: [PATCH 2/2] Patch two more locations that don't catch errors correctly --- lib/server/websocket.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/server/websocket.js b/lib/server/websocket.js index 3c1c55ff873..18081181f56 100644 --- a/lib/server/websocket.js +++ b/lib/server/websocket.js @@ -320,7 +320,13 @@ function init (env, ctx, server) { // try to find exact match ctx.store.collection(collection).find(query).toArray(function findResult (err, array) { - if (err || array.length > 0) { + if (err) { + console.error(err); + callback([]); + return; + } + + if (array.length > 0) { console.log(LOG_DEDUP + 'Exact match'); if (callback) { callback([array[0]]); @@ -363,7 +369,14 @@ function init (env, ctx, server) { // try to find similiar ctx.store.collection(collection).find(query_similiar).toArray(function findSimiliarResult (err, array) { // if found similiar just update date. next time it will match exactly - if (err || array.length > 0) { + + if (err) { + console.error(err); + callback([]); + return; + } + + if (array.length > 0) { console.log(LOG_DEDUP + 'Found similiar', array[0]); array[0].created_at = data.data.created_at; var objId = new ObjectID(array[0]._id); @@ -412,16 +425,18 @@ function init (env, ctx, server) { console.error(err); callback([]); return; - } else { - if (array.length > 0) { - console.log(LOG_DEDUP + 'Devicestatus exact match'); - if (callback) { - callback([array[0]]); - } - return; - } } + + if (array.length > 0) { + console.log(LOG_DEDUP + 'Devicestatus exact match'); + if (callback) { + callback([array[0]]); + } + return; + } + }); + ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) { if (err != null && err.message) { console.log('devicestatus insertion error: ', err.message);