From 36d84635207c7176cb1cde5d1043579ab30d526f Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Tue, 26 Jan 2021 22:04:43 +0200 Subject: [PATCH] Catch error from websocket device status dedupe (#6777) * Catch error from websocket device status dedupe * Patch two more locations that don't catch errors correctly --- lib/server/websocket.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/server/websocket.js b/lib/server/websocket.js index 9ca7c200c69..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); @@ -408,14 +421,22 @@ 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; } + + 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);