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);