Skip to content

Commit

Permalink
Catch error from websocket device status dedupe (nightscout#6777)
Browse files Browse the repository at this point in the history
* Catch error from websocket device status dedupe

* Patch two more locations that don't catch errors correctly
  • Loading branch information
sulkaharo authored and Jean-Phi37 committed Mar 18, 2021
1 parent ffd6719 commit bac1e23
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bac1e23

Please sign in to comment.